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)" )
}