1

以下代码适用于 500 条记录,我们正在批处理操作并在添加 500 条记录后提交事件存储。

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];

假设我们需要每个复制事件记录的 eventidentifier 以供将来修改,那么我们应该提交每个记录以获取 identifier 。但是这样做时,我们会收到内存警告并且设备正在重新启动。下面是示例代码:

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);

    }

}

我们需要事件标识符以供将来修改(更新/删除),因此寻找一种通过获取其事件标识符将 500 多条记录插入事件存储的方法。任何帮助将不胜感激。谢谢。

4

0 回答 0