How to be notified when received changes from only the remote CloudKit database?
Asked Answered
L

2

7

I'm building an app with Core Data and CloudKit. I need to detect when remote changes have been pushed to the app and are available in the local database to perform some work on the new data set. I've implemented .NSPersistentStoreRemoteChange like so:

NotificationCenter.default.addObserver(self, selector: #selector(self.storeRemoteChange(_:)), name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)

The problem is this notification is received even when changes haven't been received from the remote CloudKit database, such as 4 times when the app is launched, and when a modification is made to the local database. How can I know only when changes have been received from the remote database?

Luellaluelle answered 2/3, 2020 at 0:1 Comment(2)
I have only used CKContainer, not the new NSPersistentCloudKitContainer. Does it fire a push notification when you get changes like it does with CKContainer? In AppDelegate application(_:didReceiveRemoteNotification:fetchCompletionHandler:)? That would be a way to tell.Whitford
@BrianM That is deprecated, but neither that nor the new API (userNotificationCenter(_:willPresent:withCompletionHandler:)) are called.Luellaluelle
L
1

While I wasn't able to detect only this scenario, I did accomplish my needs by utilizing NSManagedObjectContextObjectsDidChangeNotification and performing both remote and local change processing when this notification is triggered.

Luellaluelle answered 7/3, 2020 at 18:33 Comment(0)
B
1

This was added in iOS 14

NSPersistentCloudKitContainer.eventChangedNotification NSPersistentCloudKitContainer.EventType.import

Brigittebriley answered 12/10, 2023 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.