1

我正在使用 NSPersistentCloudKitContainer 测试我的 CoreData + CloudKit Sync 的实现,它似乎在某些时候工作得很好。我解决了用户在 Apple 推荐的设置中打开/关闭 iCloud 的问题,但我现在遇到的问题是,当我离线测试我的应用程序时,当我通过互联网连接重新联机时,没有同步. 我有一些代码可以正确触发并检测是否存在互联网连接。但似乎仍然没有将容器选项设置为 nil 然后返回到 cloudkit 容器使其同步。到目前为止,这是我的代码:

lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
        */

        let container : NSPersistentContainer?
        container = NSPersistentCloudKitContainer(name: "Model")

        let description = NSPersistentStoreDescription(url: applicationDocumentsDirectory()!.appendingPathComponent("Model.sqlite"))
        description.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)

        //Check if user is first logged onto iCloud enabled/not - if not then set cloudkitcontainer options to nil to disable sync and use the local DB on device

        // This was from an accepted answer here: https://forums.developer.apple.com/thread/118924
        //NB This works and the code here is triggered because a switch on/off icloud in settings kills the app and makes it restart, so this code is triggered.
        if FileManager.default.ubiquityIdentityToken != nil { //logged onto iCloud*/


                       description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.my.container")
        } else {

            description.cloudKitContainerOptions = nil;

        }

        //However, turning a device to airplane mode on/off won't kill the app to trigger the above code, so have to put that check in with checking when the device is on or offline:
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.my.container")

            } else {
                description.cloudKitContainerOptions = nil

            }

        }


        //ensure migration------

        description.shouldInferMappingModelAutomatically = true
        description.shouldMigrateStoreAutomatically = true

        //----------------------

        container!.persistentStoreDescriptions = [description]

        container!.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */


                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })

        container!.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        container!.viewContext.automaticallyMergesChangesFromParent = true
        return container!
    }()

编辑

我只是从一个设备到另一个设备进行了尝试,如果一个设备在没有激活监视器实例的情况下暂时进入离线模式,它似乎可以工作(注释掉那部分代码以进行测试)。但是,如果设备长时间处于飞行模式,似乎不会发送 CloudKit 更新并且它是从其他设备同步的,而不是我正在使用的设备的最新更改......

4

0 回答 0