我有一个使用 Core Data + CloudKit 的应用程序,使用内置的 NSPersistentCloudKitContainer 同步。
它在大多数情况下运行良好,但有时它根本不会同步,没有错误或任何东西。
一些用户报告说它在 iPad 到 iPhone 上完美运行,但在 iPhone 到 iPad 上却不行。一些用户甚至报告随机丢失数据。我已经彻底检查了错误,但我真的无法弄清楚。有没有人有这个问题?有什么解决方案或至少有什么可以尝试的吗?这是数据容器代码:
lazy var container: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "<AppName>")
let directory = FileManager.default.urls(
for: .applicationSupportDirectory, in: .userDomainMask).first!
//Local data
let localStoreDescription = NSPersistentStoreDescription(url: directory.appendingPathComponent("Local.sqlite"))
localStoreDescription.configuration = "Local"
//Cloud synced data
let cloudStoreDescription = NSPersistentStoreDescription(
url: directory.appendingPathComponent("Cloud.sqlite"))
cloudStoreDescription.configuration = "Cloud"
cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(
containerIdentifier: "<ContainerIdentifier>")
container.persistentStoreDescriptions = [
cloudStoreDescription,
localStoreDescription
]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
container.viewContext.automaticallyMergesChangesFromParent = true
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return container
}()