-->

如何检测其EKevent改变(How to detect which EKevent was cha

2019-08-31 21:00发布

我得到了这个问题。 我需要知道什么时候在我的EventStore活动发生变化,所以这种情况下,我使用EKEventStoreChangedNotification但这个通知回我难以理解的字典中userInfo这是这个样子:

EKEventStoreChangedObjectIDsUserInfoKey = ("x-apple-eventkit:///Event/p429" );

我不知道我该如何使用这些数据来考虑访问已更改的对象。 请帮我

Answer 1:

这将检测发生变化的事件和某一日期范围内记录事件的标题。 虽然,我最终没有这样做,因为在实践中,我不知道的时间范围。 我需要所有我的工作的事件,这意味着我需要反正刷新它们,因为对象ID可能已经改变比较。 这最终使每一个事件并不是那么有用,现在我只是刷新每隔几秒钟变化时进来,忽略细节。 我希望苹果改进了这些通知。

#pragma mark - Calendar Changed
- (void)calendarChanged:(NSNotification *)notification {
    EKEventStore *ekEventStore = notification.object;

    NSDate *now = [NSDate date];
    NSDateComponents *offsetComponents = [NSDateComponents new];
    [offsetComponents setDay:0];
    [offsetComponents setMonth:4];
    [offsetComponents setYear:0];
    NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:now options:0];

    NSArray *ekEventStoreChangedObjectIDArray = [notification.userInfo objectForKey:@"EKEventStoreChangedObjectIDsUserInfoKey"];
    NSPredicate *predicate = [ekEventStore    predicateForEventsWithStartDate:now
                                                                  endDate:endDate
                                                                calendars:nil];
    // Loop through all events in range
    [ekEventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *ekEvent, 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);
                *stop = YES;
            }
        }];
    }];
}


文章来源: How to detect which EKevent was changed