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:
- Both apps have the same appGroup ID setup
- App group is enabled with the entitlement in both targets
- 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.