I am trying to access same model container inside Observable class which i have provided to Scene
var body: some Scene {
WindowGroup {
ContentView()
}.modelContainer(for: [Product.self]
I can access this model inside View as following
var products: [Product]
@Query(sort: \Product.name)
AND
@Environment(\.modelContext) private var modelContext
But how can i access same model inside
@Observable public class ProductStore{
//how to access same model context OR
//@Query model inside this class throughs same container
}
IN CORE DATA
class PersistentStore: ObservableObject {
var context: NSManagedObjectContext { persistentContainer.viewContext }
I am wondering how i can do it in @Observable class
@Observable public class ProductStore{ // get same context as declared in App first launch }
I do it like so
Later in your code you can use container to fetch your entities.