I am trying to test CloudKit for sync, and am testing with the Xcode (15.4) SwiftUI templates (CoreData and SwiftData variants) and also with the CKSyncEngine sample-cloudkit-sync-engine example.
I am then testing sync by running app in my MacBook and my iPhone. Generally sync is working, but the behavior is very different when syncing changes from macOS -> iOS compared to from iOS -> macOS:
- I open app on by my MacBook and iPhone
- I add item on my MacBook, it shows up a second later on iPhone
- On the other hand, if I add item on my iPhone it never shows up on my MacBook until I send app to background and then bring back to foreground.
Is this known/expected behavior? Any way to fix?
I assume this is a CKSyncEngine issue since the behavior is the same across all the different app setups and I think they all have CKSyncEngine in common.
For the sample-cloudkit-sync-engine example I have also tried calling try await self.database.syncEngine.fetchChanges(.init(scope: .all))
on the mac, but that doesn't seem to help. I still need to put app into background and then bring back to foreground before it will show changes.
I would expect changes to show up in my Mac UI without having to send app to background.
Yeah, I see people reporting that CloudKit synchronization on macOS doesn't happen as quickly as it does on iOS, and that it only happens when the app comes to the foreground.
I don't think that is really a bug because CloudKit was designed to synchronize only when it determines appropriate. In this case, it is that CloudKit on macOS chooses to synchronize when your app comes to the foreground. To be very clear, bringing your app to the foreground doesn't always trigger a synchronization, because a synchronization may be throttled if the rate is too high. For more information, see:
In the production environment, that may not be a real issue, because people typically don't put their devices side by side to see the synchronization. Instead, they typically use your app on one device, and switch to another device some time later. When they switch the device and bring your app up, the synchronization happens.
If you do see that an issue in your use case, I suggest that you file a feedback report with your concrete use cases for the CloudKit team to consider.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.