A demo about the problem of [NSPersistentCloudKitContainer stop sync when CKErrorDomain 12 error occurred]

Problem Description

My app uses NSPersistentCloudKitContainer to implement Coredata and iCloud synchronization data

Recently, I received feedback from online users about data synchronization errors, and the error code is CKErrorDomain 12 .

In the App, I use NSPersistentCloudKitContainer.eventChangedNotification to monitor the synchronization status, the following is the specific code

NotificationCenter.default.publisher(for: NSPersistentCloudKitContainer.eventChangedNotification)
            .sink(receiveValue: { notification in
                if let cloudEvent = notification.userInfo?[NSPersistentCloudKitContainer.eventNotificationUserInfoKey]
                    as? NSPersistentCloudKitContainer.Event {
                    let event = SyncEvent(from: cloudEvent) 
                }
            })
            .store(in: &disposables)

When the user feedbacks that the data cannot be synchronized, it can be seen from the above code that an error occurred when CoreData + iCloud was exporting data.

At the same time, cloudKitEvent.error does not contain any error information, only CKErrorDomain 12 this information, I don’t know how to troubleshoot the error at all.

What's even more frightening is that when the CKErrorDomain 12 error occurs, the app's synchronization service will stop immediately. Trying to restart the app or restart the phone, and turn off turn on iCloud synchronization in the system, will not make the synchronization service work again.

Only uninstalling and reinstalling can completely solve this problem, but users will also lose some data after uninstalling, because when the error occurs, any data generated by the user has not been successfully synchronized to iCloud, so these data will be lost after uninstalling.

The problem that needs to be solved at present is how to restore the synchronization service to normal when the online user encounters CKErrorDomain 12 and the synchronization service stops.

Steps to reproduce

  1. Click the bottom button to add two items
  2. Slide an item and select edit
  3. Will trigger CKErrorDomain 12 error

In this demo, closing the App completely from the background and opening it several times will automatically fix this problem, but according to the feedback from users in the production environment, after encountering the CKErrorDomain 12 error, restarting the App or the phone multiple times cannot automatically fix this problem.