0

I'm working on an Swift App which runs on iOS, macOS and watchOS (Xcode) using CloudKit (see code for the initialization of the CloudKit-Persistency-Container below). The app works great: the data gets synced via iCould on all devices.

My problem: sync/mirroring on watchOS is quite slow...and the watchOS has to be open. Is there a way to process the sync/mirroring on watchOS in background (automatically)? For iOS, I have added 'Background fetch' and 'Background processing' in the application target definition...does not exist for watchOS :(

Thanks and best regards, Lars.

init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "TestersTest")
        if inMemory {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        
        let l_store = NSPersistentContainer.defaultDirectoryURL()
        print(l_store)
        
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("###\(#function): Failed to retrieve a persistent store description.")
        }
        
        let url = container.persistentStoreDescriptions.first!.url
        print(url!)
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        container.viewContext.undoManager = nil
        container.viewContext.shouldDeleteInaccessibleFaults = true
        
        description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
        description.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
        description.setOption(true as NSNumber, forKey: NSInferMappingModelAutomaticallyOption)
        container.viewContext.transactionAuthor = "TestersTest"
        try?  container.viewContext.setQueryGenerationFrom(.current)
        
        
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        
        NotificationCenter.default.addObserver(self, selector: #selector(backgroundContextDidSave(notification:)), name: .NSManagedObjectContextDidSave, object: nil)
        
        print( "merge policy: \(container.viewContext.mergePolicy)" )
        
        backgoundContext = container.newBackgroundContext()
        
        print( "merge policy background: \(backgoundContext!.mergePolicy)" )
        
        backgoundContext!.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        
        backgoundContext!.automaticallyMergesChangesFromParent = true
        backgoundContext!.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        backgoundContext!.undoManager = nil
        backgoundContext!.shouldDeleteInaccessibleFaults = true
    
        
        print( "merge policy background: \(backgoundContext!.mergePolicy)" )
        
    }
LarsG
  • 1
  • 1
  • I've hit the same issue and end up sending the data from the phone app via `WatchConnectivity` `WCSession.default.sendMessage` if the watch app is live, and `WCSession.default.updateApplicationContext` otherwise. This way the watch app receives the data straight away, not relying purely on iCloud sync. – Damian Aug 16 '21 at 08:21
  • Thank you Damian. So you replace storing/managing the data on the watch via CloudKit by accessing the data on the iPhone through WatchConnectivity. I though about this as well...should work but a lot of separate coding for watchOS. The nice thing with Xcode/SwiftUI is that the same code (and even UI) can be used for all devices (watch, phone, mac). Using WatchConnectivity instead of CloudKit breaks this approach :( – LarsG Aug 17 '21 at 23:52
  • I actually do both - I've found that for some users, for some reasons the iCloud sync takes a long time or never happens, so the WatchConnectivity is a backup. – Damian Aug 18 '21 at 09:56

0 Answers0