How to force / wait for SwiftData sync on first app launch?

I have a SwiftData application that is using CloudKit. If user is on new device. How can I check and fetch data, instead of just waiting for it happen on its own randomly?

For example, I have onboarding which I do not want user to go through again if they already have an active installation.

Seems like SwiftData is severely limited in pretty much every way, specially any useful CloudKit debugging or control functionality.

Answered by DTS Engineer in 828681022

As of today, SwiftData + CloudKit is based on NSPersistentCloudKitContainer, and there is no API for apps to configure the timing for the synchronization. To observe the synchronization activities, you can use NSPersistentCloudKitContainer.Event.

The following technotes provides the whole picture of how NSPersistentCloudKitContainer works:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

You can store a boolean in UserDefaults to denote that a user has already been through onboarding.

When the app is launched and the data isn't there yet, you should have a screen that shows the data is loading.

As of today, SwiftData + CloudKit is based on NSPersistentCloudKitContainer, and there is no API for apps to configure the timing for the synchronization. To observe the synchronization activities, you can use NSPersistentCloudKitContainer.Event.

The following technotes provides the whole picture of how NSPersistentCloudKitContainer works:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

How to force / wait for SwiftData sync on first app launch?
 
 
Q