In my app the users have the possibility to synchronise data between all their iCloud devices (Core Data + CloudKit). And in this app I have a reminder feature I'd like to use this iCloud too. I tested with apple Calendar app and I scheduled a reminder and this reminder alert which was received from all iCloud devices (that have that app installed and accepted notifications) So my problem is how to schedule that reminder on all iCloud devices:
My idea is :
Subscribe to CloudKit container changes for the privateDB so apple servers send a push notifications to all devices when a 'Task' is created, updated, deleted. When this silent notification is received the app do: - update the core data cache to be sure it's updated - loop to check all 'Task' in the container and schedule/cancel a local notifications if needed.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
if IS_ICLOUD_ENABLED {
let taskSubscription = CKQuerySubscription(recordType: "CD_Task", predicate: NSPredicate(value: true), subscriptionID: "taskSubscription", options: [CKQuerySubscription.Options.firesOnRecordCreation, .firesOnRecordUpdate, .firesOnRecordDeletion])
let notificationInfo = CKSubscription.NotificationInfo()
notificationInfo.shouldSendContentAvailable = true
taskSubscription.notificationInfo = notificationInfo
CKContainer.default().privateCloudDatabase.save(taskSubscription, completionHandler: { _, error in
if error == nil {
print("task cloud subscription saved successfully")
} else {
print("task cloud subscription error occured : \(error!.localizedDescription)")
}
})
}
}
the code used to subscribe to changes. (silent notification)
But i couple do differently and I'd like to know the most optimized one: I could send in the silentNotification userInfo the 'Task' object properties need to schedule a Task and schedule/cancel it if needed directly when the notification I received but the device. With that way I don't need to update the local cache before but notification send will be way heavier right ? And some of that 'Task' just don't need to schedule a reminder or just need to be cancel so sometimes data will be sent for nothing.
There is some things I should take attention too: if the user enable iCloud while the app is running (from apple settings app), I should look if I need to schedule local notifications. if the user enable userNotifications (from apple settings app or while the app is running) I should look if I need to schedule local notifications again.
So what do you thing about that ? Which one is the best way to do ? Maybe there is a better way to do ?