Group Group Group Group Group Group Group Group Group

To prefill Core Data store when using NSPersistentCloudKitContainer?

Hello

What’s the best approach to prefill Core Data store when using NSPersistentCloudKitContainer?

I’m parsing objects from a JSON file and store them into my Core Data store.
I’m using “NSPersistentCloudKitContainer” and when I’m running the app on a different device, it also parses the JSON file and adds objects to Core Data. That results in duplicate objects.

How can I check that an entity already exists remotely?

@vision3 Thanks very much for your question!

Believe it or not, this is a very common problem with Core Data apps in general. The solution to this would typically be to set a flag on the local device using UserDefaults, and check to see if the data has been preloaded or not from the local file. In your case, there would be an extra step to include, and that is to connect with iCloud, and see if there is data to be sync’d.

Thus, step 1 would be to check and see if there is a corresponding data store in iCloud that the app can connect to, and pull the necessary data. If this is false THEN you’d run the preload function from your app to load the data from your local store.

I hope this helps.

All the best!