Post

Replies

Boosts

Views

Activity

NSPersistentCloudKitContainer: Data is gone when turning off iCloud in settings. Reappears when turning it on again.
I am using Core Data with CloudKit through NSPersistentCloudKitContainer. When doing the following, all the data disappears: Start my app (iCloud enabled) Create some items in CoreData Quit the app Turn off iCloud for my app in the iPhone's Settings Start the app again All the items added in step 2 are gone after this. When I turn iCloud on again, they reappear. I read through the documentation, but this does not seem to be expected behaviour. Also, the demo app - https://developer.apple.com/documentation/coredata/synchronizing_a_local_store_to_the_cloud for this topic does not seem to have this issue. However, I am building my CoreData-stack in the same way as they do. Searching on the web it seems that several people have the same issue. Did somebody resolve this? I read that one strategy is to swap NSPersistentCloudKitContainer with NSPersistentContainer in case iCloud is turned off - but the demo-app doesn't do this and still keeps the data even when iCloud gets turned off. So there must be another way. For reference, here's how I build my CoreData-stack (lazy var in a class): lazy var persistentContainer: NSPersistentContainer = { 		let container = NSPersistentCloudKitContainer(name: "MyAppsName") 		guard let storeDescription = container.persistentStoreDescriptions.first else { 				fatalError("Could not load store description.") 		} 		storeDescription.type = NSSQLiteStoreType 		storeDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey) 		storeDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) 		container.loadPersistentStores { (_, error) in 				if let error = error as NSError? { 						fatalError("Unresolved error when loading CoreData persistent stores: \(error), \(error.userInfo)") 				} 		} container.viewContext.automaticallyMergesChangesFromParent = true container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy return container
1
0
640
Sep ’20