提问人:shoji 提问时间:8/12/2023 最后编辑:shoji 更新时间:10/6/2023 访问量:665
如何在 Observable Store 中访问相同的 SwiftData 模型容器
How to access same SwiftData Model Container inside Observable Store
问:
我正在尝试访问我提供给场景的 Observable 类中的相同模型容器
var body: some Scene {
WindowGroup {
ContentView()
}.modelContainer(for: [Product.self]
我可以在视图中访问此模型,如下所示
var products: [Product]
@Query(sort: \Product.name)
和
@Environment(\.modelContext) private var modelContext
但是我怎样才能在里面访问相同的模型
@Observable public class ProductStore{
//how to access same model context OR
//@Query model inside this class throughs same container
}
在核心数据中
class PersistentStore: ObservableObject {
var context: NSManagedObjectContext { persistentContainer.viewContext }
我想知道我怎样才能在课堂上做到这一点@Observable
@Observable public class ProductStore{ // 获取与 App 首次启动时声明的相同上下文 }
答: 暂无答案
评论
ModelContainer
@Observable
ProductStore
.modelContainer(productStore.container)