Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data and cloudkit sync wwdc 2019 not working for beta 3

I am trying to replicate the result of WWDC talk on syncing core data with cloud kit automatically.

I tried three approaches:

  1. Making a new master slave view app and following the steps at in wwdc 2019 talk, in this case no syncing happens

  2. Downloading the sample wwdc 2019 app also in this case no symcing happens

  3. I made a small app with a small core data and a cloud kit container in this case syncing happens but I have to restart the app. I suspected it had to do with history management so observed the NSPersistentStoreRemoteChange notification not nothing receives.

Appreciate any help.

like image 238
Arash Avatar asked Jul 06 '19 12:07

Arash


2 Answers

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
    }()
  • 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 :(
like image 71
Albert Avatar answered Oct 30 '22 13:10

Albert


I found that the NSPersistentStoreRemoteChange notification is posted by the NSPersistentStoreCoordinator and not by the NSPersistentCloudKitContainer, so the following code solves the problem:

// Observe Core Data remote change notifications.
NotificationCenter.default.addObserver(
        self, selector: #selector(self.storeRemoteChange(_:)),
        name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)
like image 29
Gigi Avatar answered Oct 30 '22 14:10

Gigi