0

I'm trying to mirror Core Data to iCloud using CloudKit. When I delete the app on simulator and then run the app, the app does not show any data. I then run it again and this time it shows data. I tried it multiple times with the consistent result of first launch shows no data, but the second launch shows data.

Code for container:

lazy var persistentContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(name: "MyApp")
    container.loadPersistentStores(completionHandler: { (_, error) in
        guard let error = error as NSError? else { return }
        fatalError("###\(#function): Failed to load persistent stores:\(error)")
    })
    container.viewContext.automaticallyMergesChangesFromParent = true
    
    return container
}()

Code for fetching:

let fetchRequest: NSFetchRequest<MyModel> = MyModel.fetchRequest()
let results = try? context.fetch(fetchRequest)
matchifang
  • 5,190
  • 12
  • 47
  • 76

1 Answers1

0

I found the issue. It's with the way I'm fetching from Core Data. It works when I use a NSFetchedResultsController where I get updates of changes in iCloud in the delegate method: controllerWillChangeContent

matchifang
  • 5,190
  • 12
  • 47
  • 76