SwiftData and correct setup for AppGroup

Hi All,

I am using SwiftData for my new app, that is going to have also Extension and Apple Watch app companion.

At the moment I am configuring the iOS/iPadOS app as following:

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)
    }

I did the same thing both on the iOS/iPadOS code as well as in the AppleWatch app:

Apple Watch App

@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 Watch app does not see the data saved from the phone. It launches without issues, but the items returned are 0.

I doubled checked that:

  1. Both apps have the same appGroup ID setup
  2. App group is enabled with the entitlement in both targets
  3. The AppGroupID used in both apps is the one defined in sharedAppContainerIdentifier: "my.appgroup.id"

Are those 3 steps enough to share the SwiftData DB among targets (extensions/watch app/ ios...) or there is something I am missing here?

Thanks for your help in advance.

Answered by SkinSoftware in 758305022

FYI:

I was trying to use AppGroup to share data between devices.

That is no longer possible.

Either you use Watch Connectivity or iCloud.

Accepted Answer

FYI:

I was trying to use AppGroup to share data between devices.

That is no longer possible.

Either you use Watch Connectivity or iCloud.

To share data between iOS/watchOS app and extension, @AppStorage fetches data automatically. With SwiftData, if outside SwiftUI View, the programmer needs to create a fetch descriptor and fetch data manually. Is SwiftData really better than @AppStorage in ObsersableObject?

SwiftData and correct setup for AppGroup
 
 
Q