0

以下代码有效并从 iPhone 日历数据库中删除所有日历事件。但事件删除速度非常慢:iPhone 3GS 上每秒 2 个事件。因此,如果您有 3000 个事件 - 那么擦除它们需要 25 分钟...您将如何优化此代码?也许我做错了什么?谢谢。

- (void)deleteAllCalendarEventsFromIPhone:(NSError **)error {
    EKEventStore *store = [[EKEventStore alloc] init];

    NSUInteger beginYear = 1900;
    NSUInteger endYear = 2100;

    while (beginYear < endYear) {
        NSPredicate *predicate = 
            [store predicateForEventsWithStartDate:[self createDateWithYear:beginYear month:1 day:1] 
                                           endDate:[self createDateWithYear:beginYear + 4 month:1 day:1] 
                                         calendars:nil];
        NSArray *eventList = [store eventsMatchingPredicate:predicate];
        for (EKEvent *event in eventList) {         
            success = [store removeEvent:event span:EKSpanThisEvent error:nil];
        }
        beginYear += 4;
    }
    [store release];
}
4

3 回答 3

1

iOS 5.0 has a new removeEvent: method that allows you not to commit changes until the very end. I haven't tested it yet, but that sounds to be exactly what you need here.

于 2011-09-22T08:35:24.383 回答
0

也许是因为它不是 EKEventStore 的用法!
删除所有用户事件是非常危险的......

于 2010-10-28T07:34:38.777 回答
0
-(void)deleteElementFromCalendarDB {
    CFGregorianDate gregorianStartDate, gregorianEndDate;
    CFGregorianUnits startUnits = {-2, 0, 0, 0, 0, 0}; 
    CFGregorianUnits endUnits = {1, 0, 0, 0, 0, 0};
    CFTimeZoneRef timeZone = CFTimeZoneCopySystem();
    gregorianStartDate =  CFAbsoluteTimeGetGregorianDate(
        CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(),
        timeZone,startUnits),timeZone);
    gregorianStartDate.hour = 0;
    gregorianStartDate.minute = 0;
    gregorianStartDate.second = 0;
    gregorianEndDate = CFAbsoluteTimeGetGregorianDate(
        CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(),
        timeZone, endUnits),timeZone);
    gregorianEndDate.hour = 0; 
    gregorianEndDate.minute = 0; 
    gregorianEndDate.second = 0;
    NSDate* startDate = [NSDate dateWithTimeIntervalSinceReferenceDate:
    CFGregorianDateGetAbsoluteTime(gregorianStartDate, timeZone)];
    NSDate* endDate =   [NSDate dateWithTimeIntervalSinceReferenceDate:
    CFGregorianDateGetAbsoluteTime(gregorianEndDate, timeZone)];
    CFRelease(timeZone);
    NSError *err;
    NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
        endDate:endDate
        calendars:nil];
    NSArray *eventListS = [self.eventStore eventsMatchingPredicate:predicate];
    for (EKEvent *event in eventListS) {           
        [self.eventStore removeEvent:event span:EKSpanFutureEvents error:&err];
    }
    if (err == noErr) {
        UIAlertView *alert =[[UIAlertView alloc]itWithTitle:@"All Events were deleted"                                              message:@"How about that?" 
            delegate:nil
            cancelButtonTitle:@"Okay" 
            otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
于 2011-05-10T16:39:45.370 回答