Here I am having some trouble with Core Data
and SwiftUI
.
My main view has access to some information from an entity in Core Data
.
And I want to pass this to a subview, using some kind of binding so that it can also access this data and possibly update it.
Here is one version of the code I tried.
On the main view side:
@Environment(\.managedObjectContext) var managedObjectContext
......
@FetchRequest(
entity: MyEntity.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \MyEntity.name,
ascending: true)]
) var myList: FetchedResults<MyEntity>
......
var body: some View {
VStack {
Button(action: {
..........
}).sheet(isPresented: $showingList) {
MyListView(localList: self.$myList,
moc:self.managedObjectContext,
......)
}
.....
}
On the subview side:
struct MyListView: View {
@Binding var localList: FetchedResults<MyEntity>
.....
}
In the code above, the @FetchRequest is working, getting the information I expect. But after that, I am trying to do as if @FetchRequest was behaving like a @State. And it does not work. What is the proper way to give to my subview (i.e. MyListView) access to the data provided by the @FetchRequest?
I have obviously tried a few other variations of this code before writing this post, while reading some tutorials; but it still doesn't work.