我正在研究基於CloudKit的應用程序,該應用程序使用CKSubscription通知來跟蹤對公共數據庫的更改。每當應用程序接收推送通知我檢查通知隊列CKFetchNotificationChangesOperation和標記處理之後讀取每個通知:CKFetchNotificationChangesOperation返回舊通知
__block NSMutableArray *notificationIds = [NSMutableArray new];
CKFetchNotificationChangesOperation *operation = [[CKFetchNotificationChangesOperation alloc] initWithPreviousServerChangeToken:self.serverChangeToken];
operation.notificationChangedBlock = ^(CKNotification *notification) {
[notificationIds addObject:notification.notificationID];
[self processRemoteNotification:notification withCompletionHandler:completionHandler];
};
__weak CKFetchNotificationChangesOperation *operationLocal = operation;
operation.fetchNotificationChangesCompletionBlock = ^(CKServerChangeToken *serverChangeToken, NSError *operationError) {
if (operationError) {
NSLog(@"Unable to fetch queued notifications: %@", operationError);
}
else {
self.serverChangeToken = serverChangeToken;
completionHandler(UIBackgroundFetchResultNewData);
// Mark the processed notifications as read so they're not delivered again if the token gets reset.
CKMarkNotificationsReadOperation *markReadOperation = [[CKMarkNotificationsReadOperation alloc] initWithNotificationIDsToMarkRead:[notificationIds copy]];
[notificationIds removeAllObjects];
markReadOperation.markNotificationsReadCompletionBlock = ^(NSArray *notificationIDsMarkedRead, NSError *operationError) {
if (operationError) {
NSLog(@"Unable to mark notifications read: %@", operationError);
}
else {
NSLog(@"%lu notifications marked read.", (unsigned long)[notificationIDsMarkedRead count]);
}
};
[[CKContainer defaultContainer] addOperation:markReadOperation];
if (operationLocal.moreComing) {
NSLog(@"Fetching more");
[self checkNotificationQueueWithCompletionHandler:completionHandler];
}
}
};
[[CKContainer defaultContainer] addOperation:operation];
據我所知,標誌着通知讀操作將保持它顯示了在未來的隊列中取出,甚至如果服務器更改令牌重置爲零。相反,如果應該只有1個或2個新的更改標記,則每次獲取時都會收到很多舊通知。我可以從notificationType標誌中檢測到舊的,但是我擔心它們會顯示出來。我在某個地方錯過了一步嗎?
我有這個相同的問題。 http://stackoverflow.com/questions/27007014/ios-8-cloudkit-cknotifications-keep-showing-up-marked-as-cknotificationtyperea – 2014-11-19 19:16:31
@GregMaletic你仍然有這個問題? – user2924482 2015-05-26 09:26:28
@ user2924482我不是。爲什麼它現在正在工作,而不是之前,我不確定! – 2015-05-28 17:36:38