SwiftData and CloudKit

Recently I've been working on a demo project called iLibrary. The main goal was to learn more about CloudKit and SwiftData. After a while I noticed that there were some hangs/freezes when running the app in debug mode.

I first tried this with Xcode 15.4 and iOS 17.5. Here the hang only appears at the beginning, but only for a few seconds. But when I exit debug mode, there are no more hangs.

With Xcode 16 beta 4 and iOS 18 it looks completely different. In this case, the hangs and freezes are always present, whether in debug mode or not. And it's not just at the beginning, it's throughout the app. I'm aware that this is still a beta, but I still find this weird. And when I profile this I see that the main thread gets quite overloaded. Interestingly, my app doesn't have that many operations going on. So I guess something with the sync of SwiftData or my CloudKitManger where I fetch some records from the public database is not running fine.

Lastly, I wanted to delete the iCloud app data. So I went to Settings and tried to delete it, but it didn't work. Is this normal?

Does anyone have any idea what this could be? Or has anyone encountered this problem as well? I'd appreciate any support.

My project: https://github.com/romanindermuehle/iLibrary

Also finding hangs on main thread (100%) which depend on where and how the app is launched. Does not seem to be app code issue.

Experiencing the same thing. Any iCloud sync operation following an insert or modification of SwiftData objects freeze the UI. It is infuriating.

I seem to be having a similar issue. My app would lag or glitch a bit if I scrolled down immediately after the app launched. I thought it was the way I was calculating some data and presenting it, but I realized that it stopped once CloudKit stopped outputting stuff to the console. If I wait for that to stop, it doesn't lag. If I disconnect my phone from a network, so it can't sync, it doesn't lag.

I am having the same issues with SwiftData and CloudKit:

  • My app freezes without throwing errors
  • I can't delete iCloud data

Additionally, I'm seeing weird behavior with local updates being reverted after about 10 seconds. I'm using an isDeleted bool to soft-delete photos from the UI, and they reappear after 10 seconds. I can see the isDeleted bool switching from 0 -> 1 -> 0 when inspecting the default SwiftData SQLite instance

I have the same problem.

This is 100% cloud kit sync causing issues. When I configure my model not to use cloud kit everything is perfect. But when I turn it back on glitches happen. Here is the code you can use to test:

let configuration = ModelConfiguration(
  isStoredInMemoryOnly: false,
  allowsSave: true,
  cloudKitDatabase: isSyncOff
    ? .none // <- NO GLITCHES
    : .private(Cloud.identifier) // <- Hangs and lags
)

I have also noticed error messaged reported by CloudKit:

Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.

There seems to be a threading issue internally. Maybe some stuff runs on Main Thread while updates are not. Maybe the fact that background queue is trying to update on the main queue is causing the glitching. However, I did not find a workaround.

I have managed to minimize the effect by turning of automatic saving on the ManagedContext and only when needed calling the .save() and checking using .hasChanges(). However, that did not help solve the issue of lagging UI when the app moves to active scenePhase since the CloudKit sync will trigger on scenePhase change on its own.

I am not sure what else to do then to wait for a fix from Apple...

If you have a solution please share it this is making my app experience for users horrible.

SwiftData and CloudKit
 
 
Q