核心数据和CloudKit同步WWWDC 2019不适用于Beta 3

Ara*_*ash 3 synchronization core-data cloudkit

我正在尝试复制有关自动将核心数据与云工具包同步的WWDC讨论的结果。

我尝试了三种方法:

  1. 制作一个新的主从视图应用程序并按照wwdc 2019演讲中的步骤进行操作,在这种情况下不会发生同步

  2. 在这种情况下,也将下载示例wwdc 2019应用程序

  3. 我制作了一个带有少量核心数据和云工具包容器的小型应用程序,在这种情况下,发生了同步,但是我必须重新启动该应用程序。我怀疑这与历史记录管理有关,因此观察到NSPersistentStoreRemoteChange通知并非一无所获。

感谢任何帮助。

小智 7

我发现 NSPersistentStoreRemoteChange 通知是由 NSPersistentStoreCoordinator 发布的,而不是由 NSPersistentCloudKitContainer 发布的,所以下面的代码解决了这个问题:

// Observe Core Data remote change notifications.
NotificationCenter.default.addObserver(
        self, selector: #selector(self.storeRemoteChange(_:)),
        name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)
Run Code Online (Sandbox Code Playgroud)


Alb*_*ert 5

I also played around with CoreData and iCloud and it work perfectly. I would like to list some important points that may help you go further:

  • You have to run the app on a real device with iCloud Acc We can now test iCloud Sync on Simulator, but it will not get notification automatically. We have to trigger manually by select Debug > Trigger iCloud Sync
  • Make sure you added Push Notification and iCloud capability to your app. Make sure that you don't Dave issue with iCloud container (in this case, you will see red text on iCloud session in Xcode)
  • In order to refresh the view automatically, you need to add this line into your Core Data Stack: container.viewContext.automaticallyMergesChangesFromParent = true.

Code:

public lazy var persistentContainer: NSPersistentCloudKitContainer = {
        /*
         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 = NSPersistentCloudKitContainer(name: self.modelName)
        container.viewContext.automaticallyMergesChangesFromParent = true
        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)")
            }
        })
        return container
    }()
Run Code Online (Sandbox Code Playgroud)
  • When you add some data, normally you should see console log begin with CloudKit: CoreData+CloudKit: ..........
  • Sometimes the data is not synced immediately, in this case, I force close the app and build a new one, then the data get syncing.
  • There was one time, the data get synced after few hours :(