Swiftdata + Cloudkit + Mac OS how to configure for existing Swift Data store

Hi,

I have a mac os app that I am developing. It is backed by a SwiftData database. I'm trying to set up cloudkit so that the app's data can be shared across the user's devices. However, I'm finding that every tutorial i find online makes it sound super easy, but only discusses it from the perspective of ios.

The instructions typically say:

  1. Add the iCloud capability.
    
  2. Select CloudKit from its options.
    
  3. Press + to add a new CloudKit container, or select one of your existing ones.
    
  4. Add the Background Modes capability.
    
  5. Check the box "Remote Notifications" checkbox from its options.
    

I'm having issue with the following: I don't see background modes showing up or remote notifications checkbox since i'm making a mac os app.

If i do the first 3 steps only, when i launch my app i get an app crash while trying to load the persistent store. Here is the exact error message:

Add the iCloud capability.
Select CloudKit from its options.
Press + to add a new CloudKit container, or select one of your existing ones.
Add the Background Modes capability.
Check the box "Remote Notifications" checkbox from its options.

Any help would be greatly appreciated.

 var sharedModelContainer: ModelContainer = {
        let schema = Schema([One.self, Two.self])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        do {
            return try ModelContainer(for: schema, configurations: [modelConfiguration])
        } catch {
            fatalError("Could not create ModelContainer: \(error)")
        }
    }()

The fatal error in the catch block happens when i run the app.

Answered by DTS Engineer in 809816022

The error message doesn't contain any useful information, and so I don't have more to comment.

We don't have a documentation specific for macOS, because the configuration process is pretty much the same, except that, as you have noticed, the Background Mode is not available on macOS.

You can probably start with the following:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

The Background Mode capability is only for iOS, and so it is fine to not set up it if your app is only for maCOS.

"The fatal error in the catch block happens when i run the app."

Would you mind to share what error message you got? I am wondering if it is something else...

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

This is what the error.localizedDescription prints out:

The operation couldn’t be completed. (SwiftData.SwiftDataError error 1.)

Chronoforge/ChronoforgeApp.swift:31: Fatal error: Could not create ModelContainer: SwiftDataError(_error: SwiftData.SwiftDataError._Error.loadIssueModelContainer, _explanation: nil)

App works fine before i try to add cloudkit. After adding cloudkit it does this as soon as it launches.

What are the actual general steps for adding cloudkit to a mac app? I can't seem to find any documentation for that. Just for ios.

Accepted Answer

The error message doesn't contain any useful information, and so I don't have more to comment.

We don't have a documentation specific for macOS, because the configuration process is pretty much the same, except that, as you have noticed, the Background Mode is not available on macOS.

You can probably start with the following:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I’ll take a look at those links. Thanks for the help!

yeah that’s why I’m so lost. The error doesn’t say much. lol

One thing to mention is that the CloudKit integration requires that the SwiftData models follow some rules. For example, relationships must be optional, and unique constraints are not supported. I am wondering if it is that your SwiftData models don't follow the rules, which triggers the failure.

The following documentation provides more information in that regard:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Swiftdata + Cloudkit + Mac OS how to configure for existing Swift Data store
 
 
Q