处理EKEventStoreChangedNotification通知

时间:2013-07-23 09:21:26

标签: ios ios6 ekeventkit ekeventstore

我在我的应用中列出了这些事件。用户可以创建,编辑和删除事件。在viewDidLoad方法中,我获取了我需要的所有事件并将它们推送到数组中。它像预期的那样工作。

对于创建,编辑和删除事件,我使用EKEventEditViewControllerEKEventViewController,效果非常好。在控制器的委托方法中,我在数组上进行了我需要的更改并重新加载了我的视图。

当然,如果用户从其他应用程序(如内置日历应用程序)进行一些更改,我也想知道并处理。所以我观察EKEventStoreChangedNotification。从该通知我只得到“已经发生了变化”而不是哪个事件或来自哪个应用程序。实际上我想知道的是,如果我的应用程序或其他应用程序发生了更改,以及哪些事件已更改。由于我已经在EKEventEditViewControllerDelegate方法中处理了更改(来自我的应用程序),因此我不需要再次处理它们。

如果我不知道哪些对象已被更改,我必须获取所有这些对象。

现在我在日历中只有5个事件(开发设备),当然获取和排序所有事件并不是一个问题,但如果用户有超过1000个事件,那么可能只有一个事件发生变化就太过分了。

所以我的问题是:如何处理EKEventStoreChangedNotification

2 个答案:

答案 0 :(得分:1)

您可以通过以下代码确切地检测到哪个事件已被更改[免责声明代码不是我的想法,我在另一个Stack Overflow答案中找到了它并稍微修改了一下]。

我正在使用名为“JSCalendarManager”的lib与eventstore进行交互,在我的情况下,使用我的App创建的事件并与iCalendar同步我已将其eventIdentifier保存在本地数据库中,我可以检索我的搜索时间iCalendar中的事件并获得更改后的匹配。

+(void)iCloudStoreChanged:(NSNotification*)eventStoreChangeNotification{
NSArray* allScheduleRecords =[self getAllScheduleRecordSyncedToICalendar];

NSDate* startDate = [NSDate new];
NSDate* endDate   = [NSDate new];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

if (allScheduleRecords.count >= 2) {
    startDate = [dateFormatter dateFromString:[[allScheduleRecords firstObject] objectForKey:@"meetingTime"]];
    endDate = [dateFormatter dateFromString:[[allScheduleRecords lastObject] objectForKey:@"meetingTime"]];
}else if (allScheduleRecords.count > 0){
    startDate = [dateFormatter dateFromString:[[allScheduleRecords firstObject] objectForKey:@"meetingTime"]];

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today];
    components.day = 1;
    endDate = [gregorian dateFromComponents:components];
}else{
}

NSArray *ekEventStoreChangedObjectIDArray = [eventStoreChangeNotification.userInfo objectForKey:@"EKEventStoreChangedObjectIDsUserInfoKey"];

[calendarManager findEventsBetween:startDate
                               and:endDate
                 withSearchHandler:^(BOOL found, NSError *error, NSArray *eventsArray) {
                     [eventsArray enumerateObjectsUsingBlock:^(EKEvent *ekEvent, NSUInteger idx, BOOL *stop) {
                         // Check this event against each ekObjectID in notification
                         [ekEventStoreChangedObjectIDArray enumerateObjectsUsingBlock:^(NSString *ekEventStoreChangedObjectID, NSUInteger idx, BOOL *stop) {
                             NSObject *ekObjectID = [(NSManagedObject *)ekEvent objectID];
                             if ([ekEventStoreChangedObjectID isEqual:ekObjectID]) {
                                 // Log the event we found and stop (each event should only exist once in store)
                                 NSLog(@"calendarChanged(): Event Changed: title:%@", ekEvent.title);
                                 [self updateAppointmentForEvent:ekEvent];
                                 *stop = YES;
                             }
                         }];
                     }];
                 }];}

答案 1 :(得分:0)

您可以不仅仅更新屏幕/活动的事件,而不是获取所有事件。