以下代码适用于从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];
}
答案 0 :(得分:1)
iOS 5.0有一个新的removeEvent:方法,它允许您在最后之前不提交更改。我还没有测试过,但这听起来正是你需要的。
答案 1 :(得分:0)
也许是因为它不是EKEventStore的用法! 删除所有用户事件非常危险......
答案 2 :(得分: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];
}
}