3

I got the problem. I need to know when Events in my EventStore are changed, so for this case I use EKEventStoreChangedNotification but this notification return to me incomprehensible dictionary in userInfo It's look like this:

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

I don't know how I can use this data to taking access for changed object. Please help me

Andriy
  • 2,767
  • 2
  • 21
  • 29
Ivan Titkov
  • 359
  • 3
  • 14

1 Answers1

5

This will detect changed events and log the event titles over a date range. Although, I ended up not doing this because in practice I don't know the date range. I need to compare with all the events I'm working with, which means I need to refresh them anyway since the object IDs might have changed. This ends up making each event not so useful and now I just refresh every few seconds when changes come in and ignore the details. I hope Apple improves these notifications.

#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;
            }
        }];
    }];
}
Symmetric
  • 4,013
  • 3
  • 27
  • 33
  • 1
    any clues if this would be accepted on AppStore? `objectID` seems to be a 'private api' call in this case. – alex-i Dec 10 '13 at 17:40
  • 1
    I've only used this for experiments, so I don't know. I think I discovered EKEvents would respond to objectID by guessing they were NSManagedObjects internally (hence the typecast), but that doesn't seem to be the case looking at the superclasses. – Symmetric Dec 11 '13 at 03:51