2013-04-23 82 views
3

我遇到了問題。我需要知道什麼時候在我的EventStore活動發生變化,所以這種情況下,我使用EKEventStoreChangedNotification但這個通知回我難以理解的字典中userInfo 它看起來像這樣:如何檢測哪個EKevent發生了變化

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

我不知道我怎麼能使用這些數據來訪問已更改的對象。請幫助我

回答

5

這將檢測更改的事件並在日期範圍內記錄事件標題。雖然,我最終沒有這樣做,因爲在實踐中我不知道日期範圍。我需要與我正在使用的所有事件進行比較,這意味着我需要刷新它們,因爲對象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; 
      } 
     }]; 
    }]; 
} 
+0

任何線索,如果這將被接受AppStore?在這種情況下,objectID似乎是一個'私人api'調用。 – 2013-12-10 17:40:15

+1

我只用於實驗,所以我不知道。我想我發現EKEvents會通過猜測它們是NSManagedObjects內部(因此是類型轉換)來響應objectID,但是看起來並不是這種情況。 – Symmetric 2013-12-11 03:51:23

相關問題