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)