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.

CloudKit data is lost when reinstalling the app
 
 
Q