如何更快地删除日历事件?

发布于 2024-09-29 06:44:18 字数 952 浏览 2 评论 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];
}

Following code works and deletes all calendar events from iPhone calendar database.
But events are deleting very slow: 2 events per second on iPhone 3GS.
So if you have 3000 events - then it's 25 minutes to erase them...
How would you optimize this code? Maybe I am doing something wrong? Thanks.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

許願樹丅啲祈禱 2024-10-06 06:44:18

iOS 5.0 有一个新的removeEvent: 方法,允许您直到最后才提交更改。我还没有测试过,但这听起来正是您所需要的。

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.

江城子 2024-10-06 06:44:18

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

Maybe because it is not the usage of EKEventStore !
Removing all user event is quite dangerous ...

可爱咩 2024-10-06 06:44:18
-(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];
    }
}
-(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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文