2013-07-23 54 views
2

我在我的應用程序中列出事件。用戶可以創建,編輯和刪除事件。在viewDidLoad方法中,我獲取所需的所有事件並將它們推送到數組中。它像預期的那樣工作。處理EKEventStoreChangedNotification通知

用於創建,編輯和刪除事件我使用EKEventEditViewControllerEKEventViewController這很好。在控制器的委託方法中,我在陣列上進行了所需的更改並重新加載了我的視圖。

當然,我也想知道和處理,如果用戶從另一個應用程序(如內置的日曆應用程序)做出一些更改。所以我觀察到EKEventStoreChangedNotification。從那個通知我得到只有「發生了變化」,而不是哪個事件或從哪個應用程序。其實我想知道的是,如果更改是從我的應用程序或其他應用程序發生的,哪些事件已被更改。由於我已經處理了EKEventEditViewControllerDelegate方法中的更改(來自我的應用程序),因此我不需要再處理它們。

如果我不知道哪些對象已被更改,我必須對它們進行排序。

現在我在日曆(開發設備)中只有5個事件,當然對於所有事件的獲取和排序並不是問題,但是如果用戶的數量超過1000,那麼可能只有一個事件更改。

所以我的問題是:如何處理EKEventStoreChangedNotification

+0

Hi @mert你有沒有找到解決方案?如果可以,請更新?我也面臨類似的問題 –

回答

0

而不是獲取所有事件,你不能只更新屏幕上/活動的事件。

+0

我認爲日曆和來源也應該重點關注。 – frank

1

您可以通過以下代碼準確檢測出哪個事件已被更改[免責聲明代碼不是我的想法,我在另一個Stack Overflow答案中找到了它,並稍微修改了它]。

我使用了一個名爲「JSCalendarManager」互動與eventstore和我的情況下,作爲事件使用我的應用程序創建和使用的iCalendar同步的lib我已經救了他們的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; 
          } 
         }]; 
        }]; 
       }];} 
+0

我在NSObject * ekObjectID = [(NSManagedObject *)ekEvent objectID]上發現錯誤; 這行代碼請驅逐它 –

+0

你能告訴我你有什麼錯誤嗎? –

+0

1)未定義NsManagedObject 2)objectID在ekEvent中找不到 –