hi Dennis,
this is a little bit of quick guess, and it's not something i have used ... but say your AppManager needs a property to reference the SwiftData modelContainer:
var modelContainer: ModelContainer? // yes, make this optional
when you begin your App, instantiate your AppManager; and then use the onSetp
parameter in the .modelContainer(for: ...)
modifier to handle a callback. you'll be given a Result
type with the modelContainer, at which point you can update the AppManager's reference to the modelContainer.
so something like this
struct MyApp: App {
let appManager = AppManager()
var body: some Scene {
WindowGroup {
MainView()
.modelContainer(for: [ModelObject.self], onSetup: handleSetup)
}
}
func handleSetup(result: Result<ModelContainer, Error>) {
switch result {
case success(let modelContainer):
appManager.modelContainer = modelContainer
case .failure(let error):
print("error: \(error.localizedDescription)")
}
}
}
that could answer your main question, and you can certainly then your AppManager can execute fetch calls on demand using the mainContext of the modelContainer.
however, if you're thinking that you'd like to have an equivalent of Core Data's NSFetchedResultsController
for SwiftData, i don't think that's there yet.
hope that's of some interest,
DMG (i am not an AI)
added ater the post: there's probably a more direct way to do this, where you create the modelContainer yourself in advance of having to rely on the .modelContainer
modifier to do that for you.