Core data 在工作表中无法获取请求?
我有一个SwiftUI应用程序,它有一个主视图,这是调用一个类似Core data 在工作表中无法获取请求?,core-data,swiftui,fetchrequest,Core Data,Swiftui,Fetchrequest,我有一个SwiftUI应用程序,它有一个主视图,这是调用一个类似 this:.sheet(isPresented: $showingSheetFilter) { FilterView() } FilterView如下所示: import SwiftUI struct FilterView: View { @FetchRequest( entity: Category.entity(), sortDescriptors: [
this:.sheet(isPresented: $showingSheetFilter) {
FilterView()
}
FilterView如下所示:
import SwiftUI
struct FilterView: View {
@FetchRequest(
entity: Category.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Category.title, ascending: true)
]
) var categories: FetchedResults<Category>
var body: some View {
List {
ForEach(categories, id: \.self) { (cat: Category) in
Text(cat.title!)
}
}.onAppear {
print(self.categories.count)
}
}
}
在舞台上。如果我去掉onAppear,我会在ForEach中得到它
为什么会发生这种情况?正如我在我的环境中对问题所做的评论一样,一切都很好。我看到的唯一区别是,我在工作表中显式地传递视图中的上下文,如下所示
Button("Test Sheet") { self.isSheet = true }
.sheet(isPresented: $isSheet) {
TestListView().environment(\.managedObjectContext, context)
}
请尝试做同样的事情。以防万一。在我的测试数据库中,同样的场景也可以。使用Xcode 11.2/11.3、iOS 13.2/13.3进行测试。
Button("Test Sheet") { self.isSheet = true }
.sheet(isPresented: $isSheet) {
TestListView().environment(\.managedObjectContext, context)
}