如何更快地删除日历事件?
以下代码有效并从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
也许是因为它不是 EKEventStore 的用法!
删除所有用户事件是相当危险的......
Maybe because it is not the usage of EKEventStore !
Removing all user event is quite dangerous ...