Different Scenarios with Core Data and CloudKit

I am creating a SwiftUI app that uses CloudKit and Core Data. If the user is signed in to iCloud it will use both, if not, it will only use Core Data.

While testing I realized there are so many situations you have to take into account, ex: The user started off not signed into iCloud, then later signed in after using the app for a while. So now there's an issue with merging the cloud data and the recent core data.

I want to be able to ask the user if they want to use the backed-up data or the new data. How would I go about deleting the Core Data, and reloading the backed-up data, or vice versa.

hi,

i would think your easiest solution is to use the built-in linkage between Core Data and CloudKit using NSPersistentCloudKitContainer.

this takes care of all of your concerns: your data is stored locally in Core Data and mirrored to/from the cloud for you. everything works fine when offline or not signed in to iCloud.

just remember that in this design, the Cloud becomes the "source of truth" for your data. modify data on one device; it is synced with iCloud; and then the data is synced with your other devices, where their local Code Data store is synced to match what's in the cloud.

there would be no option for the user here to "choose" which data they want to use, as you say, "the backed-up data or the new data." once your device is online and signed into iCloud, your on-device data will be synced to be a mirror of what's in the cloud.

hope that helps,

DMG

Different Scenarios with Core Data and CloudKit
 
 
Q