SwiftData CloudKit sync on WatchOS 10

Dear all,

Since It is my first apple watch application I just realized that is not possible to use AppGroups to sync between devices and after I spent 5 days in debugging something impossible to do 🙁 ...

... I am now trying to sync between iPhone and AppleWatch using iCloud (CloudKit), SwiftData and the following code (on iOS 17 and Watch OS 10 - beta 3):

My Persistency Library

public let fullSchema: Schema = Schema([
        SWDItem.self,
        SWDPackage.self
    ])
    
    public func makeContainer() -> ModelContainer {
        
        let configuration = ModelConfiguration(
            "SWDMyApp",
            schema: fullSchema,
            sharedAppContainerIdentifier: "my.appgroup.id",
            cloudKitContainerIdentifier: "iCloud.my.icloud.id")
        
        
        return try! ModelContainer(for: fullSchema, configuration)
         
    }

And at the start of the app there is the following configuration as described in SwiftData documentation:

iOS/iPad OS

@main
struct MyApp: App {

    let container = SWDModelContainer().makeContainer()

 [...]

    var body: some Scene {
        WindowGroup {
            LaunchScreen()
        }
        .modelContainer(container)
    }

And the same for AppleWatch

Watch OS 10

@main
struct MyAppWatch: App {
    
    let container = SWDModelContainer().makeContainer()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(container)
    }
}

The model configuration is defined in a Swift Package. Therefore the code to configure the model container is exactly the same.

The app works perfectly and can store data successfully. Data are persisted.

The iCloud sync works between my iPad and my iPhone, BUT NOTHING ARRIVES TO APPLE WATCH.

  1. The entitlements of the applewatch is the following:

Where iCloud container and AppGroup container match the iOS ones.

  1. I am logged in with the same iCloud account in all devices
  2. The watch is connected to WiFi

Why the SwiftData sync does not work on AppleWatch? What else can I check to make it work?

Thanks a lot in advance for your help

Accepted Reply

For some reason even if I changed the cloudKitContainerIdentifier: "iCloud.my.icloud.id" the app was still using the old container.

Not sure if it's a bug on Swift Data or intended behavior.

By reverting the old container id now works.

Replies

For some reason even if I changed the cloudKitContainerIdentifier: "iCloud.my.icloud.id" the app was still using the old container.

Not sure if it's a bug on Swift Data or intended behavior.

By reverting the old container id now works.

Sorry @SkinSoftware but I'm having the same problem and I'm not shure I understand how you did it. In my case it should be even simpler because I'm using only on single App swift file for Apple Watch and iOS.

In my case I'm seeing two different data sets, one for iPhone and another for Apple Watch, but my intention is to have only one. Would you or someone bee able to explain how to use the same data in iOS and Apple Watch, using SwiftData and CloudKit?

Any hint/help would be much appreciated.