1

我有一个使用 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
}()
4

1 回答 1

0

取决于您何时开始使用NSPersistentCloudKitContainer您看到的问题,这很可能与我在 iOS 15 早期测试版中发现的错误有关,该错误在 iOS 15 的公开发布版本中仍然存在 - 假设问题仅被用户看到运行 iOS 15?

苹果已经承认这里存在问题,他们目前正在调查中。

见这里: NSPersistentCloudkitContainer 内存泄漏 -> 崩溃?(iOS 15 测试版 4、5、6 和 7...)

于 2021-10-12T10:30:21.490 回答