iOS7 - device restaring when copying 500 records to eventstore with Commit=YES

半世苍凉 提交于 2019-12-24 15:35:44

问题


The below code will work fine with 500 records, we are batching the operation and commit the eventstore after adding 500 records.

EKEventStore *eventStore = [[EKEventStore alloc] init];
for(int i=0 ; i< 500 ; i++){

    EKCalendar *calendarDef = [eventStore defaultCalendarForNewEvents];
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    [event setCalendar:calendarDef];

    //set values to this event. like title, notes, startDate, endDate, location
    event.title = [NSString stringWithFormat:@"testno-%i", i];
    event.startDate = [NSDate date];
    event.endDate = [[NSDate date] dateByAddingTimeInterval:180];


    NSError *err1 = nil;
    BOOL isStoredd = [eventStore saveEvent:event span:EKSpanThisEvent commit:NO error:&err1];

    NSLog(@"item %i", i);
    if(isStoredd){

        NSLog(@"stored");
    }else{
        NSLog(@"event saved error = %@",err1);

    }

}
[eventStore commit:NULL];

Suppose we need the eventidentifier of each copying event record for future modifications, then we should commit each record to get the identifier . But when doing this we are getting memory warning and device is restarting.. Below is the sample code:

EKEventStore *eventStore = [[EKEventStore alloc] init];
for(int i=0 ; i< 500 ; i++){

    NSString *eventIde = nil;
    EKCalendar *calendarDef = [eventStore defaultCalendarForNewEvents];
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    [event setCalendar:calendarDef];

    //set values to this event. like title, notes, startDate, endDate, location
    event.title = [NSString stringWithFormat:@"testno-%i", i];
    event.startDate = [NSDate date];
    event.endDate = [[NSDate date] dateByAddingTimeInterval:180];


    NSError *err1 = nil;
    BOOL isStoredd = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err1];

    NSLog(@"item %i", i);
    if(isStoredd){

        eventIde = event.eventIdentifier;
        //storing the eventIde to application's database to modify/delete the event later.
        NSLog(@"stored identifier %@", eventIde);
    }else{
        NSLog(@"event saved error = %@",err1);

    }

}

We need the eventidentifier for future modifications(update/delete), So looking for a way to insert 500+ records into eventstore by gettng its eventidentifier. Any help would be greatly appreciated. Thanks.

来源:https://stackoverflow.com/questions/19682807/ios7-device-restaring-when-copying-500-records-to-eventstore-with-commit-yes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!