SwiftData iCloud sync breaks after disabling and re-enabling iCloud

A fairly simple ModelContainer:

    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Model1.self,
            Model2.self,
            Model3.self
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false, cloudKitDatabase: .automatic)

        do {
            let container = try ModelContainer(for: schema, migrationPlan: MigrationPlan.self, configurations: [modelConfiguration])
            return container
        } catch {
            fatalError("Error: Could not create ModelContainer: \(error)")
        }
    }()

After upgrading to macOS 15 and disabling/enabling iCloud for the app the sync stopped working on Mac. The steps:

  1. Go to System Settings > Apple Account > iCloud > Saved to iCloud > See all
  2. find the App and disable iCloud. After this synced items are removed from the app and some errors thrown in the console ('..unable to initialize without an iCloud account...')
  3. Re-enable the iCloud setting

This error appears in the console:

CoreData: error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](612): <NSCloudKitMirroringDelegate: 0x6000020dc1e0> - resetting internal state after error: Error Domain=NSCocoaErrorDomain Code=134415 "(null)"

On macOS Sonoma the items are synced back to the app and the sync is restored, but on Sequoia they don't come back and the sync is not working. I tried resetting the container, deleting all data - no help.

Submitted FB15455847

Answered by DTS Engineer in 808981022

The #3 error says that your app failed to connect to cloudd because of a "Sandbox restriction", which indicates an entitlement issue, which ... doesn't make sense to me if the same thing works well on Sonoma (because that proves that the entitlements are correctly configured).

Would you mind to double check that, when the error occurs:

Also, I am wondering how you ran your app when testing the issue – Did you run the app with Xcode, or did you do it by double clicking a TestFlight version? In the latter case, was the app in the Applications folder?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Update:

Looks like the bug exists even without disabling and enabling iCloud. It can be reproduced by starting a new project from Xcode templates: New Project > Multiplatform app with SwiftData and CloudKit

Looks like the sync is broken on macOS 15.0.1 (although works on iOS 18 and 18.1 and on macOS 14.7)

Thanks for filing the feedback report. Other than that, if you would like to figure out what really happens when synchronization fails, please follow this technote to capture and analyze a sysdiagnose:

I am guessing that it is the system delaying or throttling the synchronization for some reason (reasonable or not), and am curious to see your findings.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for the suggestion, @DTS Engineer, I saved the sysdiagnose and filtered it to errors. This is using a basic example project from Xcode. I'm running it on iPhone and Mac and when saving items from iPhone I can see them appear in CloudKit database, but they don't get synced to the Mac.

And these are 5 errors that are looping when the Mac is trying to sync (I assume):

1

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1242): <NSCloudKitMirroringDelegate: 0x60000377c000>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x13dc04080> (URL: file:///Users/admin/Library/Containers/ca.maybelater.SequoiaSync2/Data/Library/Application%20Support/default.store)
<CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

2

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2312): <NSCloudKitMirroringDelegate: 0x60000377c000> - Attempting recovery from error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

3

Error retrieving daemon to get network transfer endpoint: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction." UserInfo={NSDebugDescription=The connection to service named com.apple.cloudd was invalidated: failed at lookup with error 159 - Sandbox restriction.}

4

Error getting network transfer endpoint: <CKError 0x600000600990: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

5

error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate resetAfterError:andKeepContainer:](612): <NSCloudKitMirroringDelegate: 0x60000377c000> - resetting internal state after error: <CKError 0x60000064adf0: "Service Unavailable" (6/NSCocoaErrorDomain:4099); "Error connecting to CloudKit daemon. This could happen for many reasons, for example a daemon exit, a device reboot, a race with the connection inactivity monitor, invalid entitlements, and more. Check the logs around this time to investigate the cause of this error."; Retry after 5.0 seconds>

And then it goes back to 1 and repeats.

Does it look like a specific device issue?

Accepted Answer

The #3 error says that your app failed to connect to cloudd because of a "Sandbox restriction", which indicates an entitlement issue, which ... doesn't make sense to me if the same thing works well on Sonoma (because that proves that the entitlements are correctly configured).

Would you mind to double check that, when the error occurs:

Also, I am wondering how you ran your app when testing the issue – Did you run the app with Xcode, or did you do it by double clicking a TestFlight version? In the latter case, was the app in the Applications folder?

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

After following a combination of these steps I got the sync back, although I ended up creating a new CloudKit container too. What I did:

  • removed the apps from all devices
  • made sure the store was removed on Mac
  • logged out of iCloud on Mac, logged back in and rebooted (I don't think I did reboot before)
  • unchecked and rechecked 'Automatically manage signing' in Signing & Capabilities (also didn't do that before)
  • added a new CloudKit container and verified that it's assigned to the correct App ID.

I previously tried adding a new container, but without these other steps it didn't work.

So looks like it was a local issue after all. I moved the project from one machine to another recently and probably that broke the sync somehow. The app was run from Xcode.

Thanks for your help, @DTS Engineer!

UPDATE: After some time the sync fails again for no obvious reason and here is the error that appears in the console for a Mac app:

CoreData: warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate finishedAutomatedRequestWithResult:](3599): Finished request '<NSCloudKitMirroringExportRequest: 0x600002d8c140> EF99499F-210C-4DB0-98F9-C10563CBD3D9' with result: <NSCloudKitMirroringResult: 0x6000000bcb10> storeIdentifier: 01B8CAF5-154B-47B9-81CC-5850C07D2836 success: 0 madeChanges: 0 error: Error Domain=NSCocoaErrorDomain Code=134417 "Request '<NSCloudKitMirroringExportRequest: 0x600002d8c140> EF99499F-210C-4DB0-98F9-C10563CBD3D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'." UserInfo={NSLocalizedFailureReason=Request '<NSCloudKitMirroringExportRequest: 0x600002d8c140> EF99499F-210C-4DB0-98F9-C10563CBD3D9' was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest'.}

Is this a sync attempt that's being cancelled?

I added a sample project to the original submitted feedback, not sure how to attach it here.

"... was cancelled because there is already a pending request of type 'NSCloudKitMirroringExportRequest..."

This message doesn't quite matter, as discussed in Execute the export.

"After some time the sync fails again for no obvious reason and here is the error that appears in the console for a Mac app"

To get to the bottom in this situation, you might still want to look into a sysdiagnose. The whole analysis process is discussed in TN3163: Understanding the synchronization of NSPersistentCloudKitContainer.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Looks like the problem is tied to this specific Mac, because I updated another machine from 14.7 to 15.0.1 and the sync doesn't break there so far 🤔 As for sysdiagnose, I did a couple of those and not finding anything obvious.

Well, after a while the other Mac with macOS 15.0.1 stopped syncing too. So the original report is still valid and I can reproduce it on two Macs with 15.0.1 and two iOS devices with iOS 18.1 b7. There is a simple project attached to FB15455847 which fails to sync after disabling / enabling iCloud and adding new items from all devices.

Going through sysdiagnose again and following the steps in the article up to Execute the import

All previous steps look fine but when it comes to enqueuing the request:

default  … 14:14:15.841814 … YourCoolApp CloudKit: CoreData+CloudKit: 
-[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke(1003): 
<NSCloudKitMirroringDelegate: …>: enqueuing request: 
<NSCloudKitMirroringImportRequest: …> 2D00459F-8C08-4E9A-BC44-FCE1CD29A550

the enqueue fails with this:

warning: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _enqueueRequest:]_block_invoke_2(1030): Failed to enqueue request: <NSCloudKitMirroringImportRequest: 0x600001108690> 7F8489DA-CBB1-47E7-AE6D-EF6F107B1E29
Error Domain=NSCocoaErrorDomain Code=134417 "Request '<NSCloudKitMirroringImportRequest: 0x600001108690> 7F8489DA-CBB1-47E7-AE6D-EF6F107B1E29' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'." UserInfo={NSLocalizedFailureReason=Request '<NSCloudKitMirroringImportRequest: 0x600001108690> 7F8489DA-CBB1-47E7-AE6D-EF6F107B1E29' was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'.}

and then it just loops over trying to execute that NSCloudKitMirroringImportRequest and failing:

Request '<NSCloudKitMirroringImportRequest> ... was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'. and the import never happens.

@DTS Engineer is it possible to escalate this? It's still happening in macOS 15.1.1 and I created a simple starter project where the problem is reproducible.

Need to run the app on iOS and macOS and after a few minutes of using the app (creating items on iPhone and Mac) the sync breaks:

https://www.icloud.com/iclouddrive/0f3lh5CfIuaE5Tb54PC5-gycg#SequoiaSync2

the logs only show this failure that I mentioned above:

Request '<NSCloudKitMirroringImportRequest> ... was cancelled because there is already a pending request of type 'NSCloudKitMirroringImportRequest'.
SwiftData iCloud sync breaks after disabling and re-enabling iCloud
 
 
Q