We are trying to solve for the following condition with SwiftData + CloudKit:
- Lots of data in CloudKit
- Perform "app-reset" to clear data & App settings and start fresh.
- Reset data models with
try modelContext.delete(model:_)
myModel.count() confirms local deletion (0 records); but iCloud Console shows expectedly slow process to delete. - Old CloudKit data is returning during the On Boarding process.
Questions:
• Would making a new iCloud Zone for each reset work around this, as the new zone would be empty? We're having trouble finding details about how to do this with SwiftData.
• Would CKSyncEngine
have a benefit over the default SwiftData methods?
Open to hearing if anyone has experienced a similar challenge and how you worked around it!
SwiftData + CloudKit doesn't expose any CloudKit data structure, and so you will need to purge the data with your own code. Given that today's SwiftData + CloudKit uses NSPersistentCloudKitContainer under the hood, I'd consider the following flow:
-
Set up an
NSPersistentCloudKitContainer
instance and use it to load the SwiftData store. -
Fetch an object from the store, and retrieve the CloudKit record ID using
recordIDForManagedObjectID:
. From there, you can grab the record's zoneID. -
Call purgeObjectsAndRecordsInZoneWithID:inPersistentStore:completion: with the record zone ID to purge the local and remote data.
-
Release all the Core Data objects.
With that, you should be able to get an empty store, use it to set up a new SwiftData model container, and start your app from the beginning.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.