CloudKit data is lost when reinstalling the app

We worked with SwiftData, and once CloudKit was integrated, the synchronization worked well. Even if I rerun the app, it works just as well. However, when I delete the app and reinstall it, I get a Token Expired error and CloudKit doesn't work properly.

My code is organized like this


    public lazy var modelContext: ModelContext = { ModelContext(modelContainer) }()

    private lazy var modelContainer: ModelContainer = {

        let schema = Schema([

            Entity1.self,

            Entity2.self,

            Entity3.self,

        ])

        let modelConfiguration = ModelConfiguration(

            schema: schema,

            groupContainer: .identifier("myGroupContainer"),

            cloudKitDatabase: .automatic

        )

        do {

            return try ModelContainer(for: schema, configurations: [modelConfiguration])

        } catch {

            fatalError("Could not create ModelContainer: \(error)")

        }

    }()

The error content is as follows

error: CoreData+CloudKit: -[PFCloudKitImportRecordsWorkItem fetchOperationFinishedWithError:completion:]_block_invoke(707): <PFCloudKitImporterZoneChangedWorkItem: 0x3022c0000 - <NSCloudKitMirroringImportRequest: 0x3036e7ac0> 1A7E53D4-E95B-423F-8887-66360F6D8865> {
(
    "<CKRecordZoneID: 0x301bb1bf0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>"
)
} - Fetch finished with error:
<CKError 0x301bb5650: "Partial Failure" (2/1011); "Couldn't fetch some items when fetching changes"; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A; container ID = "MyContainerID"; partial errors: {
	com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x301bb1830: "Change Token Expired" (21/2026); server message = "client knowledge differs from server knowledge"; op = 515034AC3ADC4348; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A>
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _importFinishedWithResult:importer:](1390): <PFCloudKitImporter: 0x3000a1240>: Import failed with error:
<CKError 0x301bb5650: "Partial Failure" (2/1011); "Couldn't fetch some items when fetching changes"; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A; container ID = "MyContainerID"; partial errors: {
	com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x301bb1830: "Change Token Expired" (21/2026); server message = "client knowledge differs from server knowledge"; op = 515034AC3ADC4348; uuid = 3F346302-C3EE-4F72-820C-988287C92C0A>
}>

Forcing the ModelContainer to be reinitialized fixes the problem,

  1. it's a problem to get this error in the first place,
  2. the error doesn't even go to fatal for me, so I don't even know how to verify that it's happening.

Is there something I'm doing wrong, or do you have any good ideas for solving the same problem?

Answered by DTS Engineer in 788547022

The code doesn't seem to have any obvious issue. When detecting an invalid token, which happens when the app is deleted and then re-installed, the system is supposed to schedule an initial synchronization, which downloads the data from the CloudKit database and transforms it to the SwiftData store. The process may take a while depending on the size of the data set. If you don't see that happening, consider filing a feedback report.

In your case, the store locates in the App Group container, which may add a bit more complexity because when your app is removed, the store may still be there if other apps or extensions that use the container still exist. You can try not using the App Group container, and verify if the issue is still there.

The code doesn't seem to have any obvious issue. When detecting an invalid token, which happens when the app is deleted and then re-installed, the system is supposed to schedule an initial synchronization, which downloads the data from the CloudKit database and transforms it to the SwiftData store. The process may take a while depending on the size of the data set. If you don't see that happening, consider filing a feedback report.

In your case, the store locates in the App Group container, which may add a bit more complexity because when your app is removed, the store may still be there if other apps or extensions that use the container still exist. You can try not using the App Group container, and verify if the issue is still there.

I'm having this exact same issue and just can't get it fixed. Identical error message to what is posted above. And I'm not creating a ModelConfiguration or group container as above. This issue just started happening suddenly after my app has been in the appstore for 2.5 months. And now customers are complaining. Desperate to find a solution.

I'm still experiencing this issue. Was hoping it was fixed with iOS18 but I'm still getting the "Change token expired" messages when reinstalling from the app store after deleting the app. Strangely, I see the error messages when deleting the app and installing from Xcode, but several seconds later it will eventually do the iCloud sync. But when just installing from the app store again it will never complete and fail. Can't figure out how to detect when this is happening and how to force an iCloud sync. I understand it's supposed to be automatic but not working that way.

@deokhwa.lee hey I was hoping you could share how you resolved this issue. When you said "Forcing the ModelContainer to be reinitialized fixes the problem".. can you please explain how you do a force reinitialization of the ModelContainer? I've tried everything and can't get that workaround to work. I'm desperate to figure this out as I have frustrated customers waiting. Would be very grateful to you, thanks.

CloudKit data is lost when reinstalling the app
 
 
Q