SwiftData CloudKit integration seems to log out or deregister

Friends,

I have created an App in SwiftData that uses CloudKit to sync between devices. I am using Xcode 15.0.1, and iOS 17.1.1. For further context, I am calling modelContext.save() with every change that I want to synchronise, and this seems to call a CloudKit save, as expected, within the development environment.

The synchronisation works great in the development environment with no issues, it updates fairly quickly and I use @Query to manage SwiftUI updates in the most direct way possible. It is a complex model with many relationships, and I call @Query and filter instances to get to the instance required, rather than using traditional Bindings, as many tutorials and WWDC have suggested.

When I switch to the production environment the app synchronises between devices great using the production CloudKit container. I have set up push notification certificates, and these seem to work. The issues is that, after a few minutes, the synchronisation stops happening between devices.

When synchronisation is working, Device 1 initially saves to CloudKit, and you can see the items update in the CloudKit console. When synching stops working, Device 1 stops saving, and the CloudKit console stops updating.

If I then delete and install the app on Device 1 from TestFlight, you can see that it starts saving again, with updates to the CloudKit console as expected.

But, Device 2 does not begin updating again unless you delete the app on that device, and install again.

This behaviour seems to suggest that both devices become logged out or deregistered from CloudKit after a short period of time as a consequence of some unknown process within CloudKit or SwiftData. When logged out, each device cannot write to, or read from, CloudKit.

Now, the problem with trying to fix the issue is that the registration with CloudKit, silent push notifications, and fetches, are all managed "under the hood" by SwiftData.

In the production environment, you cannot configure any registration state or refresh registration within your code, because you don't have access to the parts of SwiftData that manage this.

I wanted to find out if this is an issue that has been encountered by other users, and if anyone can help with a solution, or perhaps a debugging strategy that I can use to find out what is happening.

Thank you all in advance.

I have a similar problem. My app saves data in SwiftData on an iPhone, and to a CloudKit private container. Works well for the iPhone. However, when running the app on an iPad signed in the same iCloud account, data are not fetched from CloudKit.

My issue is now fixed. It was that I had not deployed schema changes from development to production containers in the CloudKit console. The cause of logging out intermittently might be a response to finding an inconsistency between the data model and container.

I'm having this issue. Device#1 sends updated data to iCloud fine, but Device#2 doesn't get notified of changes consistently. Sometimes it happens, but often it does not.

SwiftData CloudKit integration seems to log out or deregister
 
 
Q