简体   繁体   English

如何检测事件存储更改以及更改的内容? 来自EKEventStoreChangedNotification

[英]How to detect event store changes and what is change? From EKEventStoreChangedNotification

how to know whether EKEvent is added or deleted? 如何知道是否添加或删除了EKEvent? so that i can schedule notification for event. 这样我就可以安排活动通知。 otherwise for deleted event also it will schedule a notification. 否则对于已删除的事件,它也会安排通知。 any help? 任何帮助?

 -(void)storeChanged:(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);

                NSLog(@"%@",ekEvent);

                NSInteger total = [[[UIApplication sharedApplication] scheduledLocalNotifications]count];
                if (total == 64) {
                    [self cancelLastNotification];
                }

                NSArray *newArray = [[NSArray alloc]initWithObjects:ekEvent, nil];
                [self scheduleNotificationForCalendarEvents:NO andEvents:newArray];

                *stop = YES;
            }
        }];
    }];

}

如果使用refresh属性,它将在删除时返回false,如果仍然可以使用,则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM