我正在开发一个使用 CloudKit 在 iOS、macOS 和 watchOS (Xcode) 上运行的 Swift 应用程序(请参阅下面的 CloudKit-Persistency-Container 初始化代码)。该应用程序运行良好:数据通过 iCould 在所有设备上同步。
我的问题:watchOS 上的同步/镜像非常慢……而且 watchOS 必须打开。有没有办法在后台(自动)处理 watchOS 上的同步/镜像?对于 iOS,我在应用程序目标定义中添加了“后台获取”和“后台处理”……watchOS 不存在 :(
谢谢和最好的问候,拉斯。
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)" )
}