Home / IOS Development / ios – CoreData with CloudKit does not work in watchOS

ios – CoreData with CloudKit does not work in watchOS



Note: This question is similar to what I asked earlier, with the difference that it was about WidgetKit and this applies to watchOS.

In my app I have one NSPersistentCloudKitContainer set up as follows:

    let container: NSPersistentCloudKitContainer

    init() {
        self.container = NSPersistentCloudKitContainer(name: "Model")

        if let storeURL = URL.storeURL(for: "group.countdown2", databaseName: "Model") {
            let storeDescription = NSPersistentStoreDescription(url: storeURL)
            self.container.persistentStoreDescriptions = [storeDescription]
        }

        self.container.persistentStoreDescriptions[0].setOption(
            true as NSNumber,
            forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey
        }

        self.container.loadPersistentStores { (_, error) in
            guard error == nil else {
                print("error loading store: (error.debugDescription)")
                return
            }
        }
        
        self.container.viewContext.automaticallyMergesChangesFromParent = true
    }

When I request a retrieval request for the context of the container in my iOS target, it works as expected. However, when I execute the same retrieval request in my watchOS target, it returns an empty array result.

In both cases, I have background mode / external alerts and iCloud / CloudKit features.

Not sure why it does not work, so help will be greatly appreciated, thanks!


Source link