Ckfetchnotificationchangesoperation Sometimes Does Not Return Update, Delete Notifications

CKSubscription of type CKSubscriptionOptionsFiresOnRecordUpdate doesn't work

Its a known bug that update notifications are not working.

[CKFetchNotificationChangesOperation sometimes does not return UPDATE, DELETE notifications
[CloudKit push notifications on record update stopped working
[https://forums.developer.apple.com/thread/7288][3]

Alternatives to UPDATE notification that does not work

I have used a similar strategy. There are some drawbacks:

  • more data (storage and transfer) usage
  • limited subscription possibilities or you have to duplicate even more data.
  • extra code in your app that you actually don't want to be there.

The only good solution to this problem is:

  • Apple should fix it...

CloudKit: delete CKSubscription is not working

Ok, this isn't the definitive answer, but need to put some code down and the comments are too short. What does it all mean; the first method fetches any outstanding notifications, the second makes sure you the same page as regarding their attention.

You need to make sure you got something like place before deleting the subscriptions ideally.

Disclaimer this is demo code; and I haven't tested it extensively. But it should work.

func fetchSubsInPlace() {
let container = CKContainer(identifier: "iCloud.ch")
let publicDB = container.publicCloudDatabase

publicDB.fetchAllSubscriptionsWithCompletionHandler({subscriptions, error in
for subscriptionObject in subscriptions! {
let subscription: CKSubscription = subscriptionObject as CKSubscription
print("subscription \(subscription)")
}
})
}

func fetchNotificationChanges() {
let operation = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)

var notificationIDsToMarkRead = [CKNotificationID]()

operation.notificationChangedBlock = { (notification: CKNotification) -> Void in
// Process each notification received
if notification.notificationType == .Query {
let queryNotification = notification as! CKQueryNotification
let reason = queryNotification.queryNotificationReason
let recordID = queryNotification.recordID

print("reason \(reason)")
print("recordID \(recordID)")
// Do your process here depending on the reason of the change

// Add the notification id to the array of processed notifications to mark them as read
notificationIDsToMarkRead.append(queryNotification.notificationID!)
}
}

operation.fetchNotificationChangesCompletionBlock = { (serverChangeToken: CKServerChangeToken?, operationError: NSError?) -> Void in
guard operationError == nil else {
// Handle the error here
return
}

// Mark the notifications as read to avoid processing them again
let markOperation = CKMarkNotificationsReadOperation(notificationIDsToMarkRead: notificationIDsToMarkRead)
markOperation.markNotificationsReadCompletionBlock = { (notificationIDsMarkedRead: [CKNotificationID]?, operationError: NSError?) -> Void in
guard operationError == nil else {
// Handle the error here
return
}
}

let operationQueue = NSOperationQueue()
operationQueue.addOperation(markOperation)
}

let operationQueue = NSOperationQueue()
operationQueue.addOperation(operation)
}

}


Related Topics



Leave a reply



Submit