I use @FetchRequest for both of my Tabs, but a bit differently.
In the Tab 1 my View has @FetchRequest that directly fetches instances of MyPersistenceObject:
struct Tab1: View {
		@Environment(\.managedObjectContext) var managedObjectContext
		@FetchRequest(entity: MyPersistenceObject.entity(),
sortDescriptors: [])
var myPersistenceObjects: FetchedResults<MyPersistenceObject>
		...
		ForEach(myPersistenceObjects, id: \.self) { myPersistenceObject in
				 ListViewTab1(myPersistenceObject: myPersistenceObject)
		}
		
Instances of myPersistenceObject are then passed to my ListViewTab1 View (see above).
But, for the Tab 2, I also use @FetchRequest, but I am not directly fetching MyPersistenceObject but Parents of parents of my MyPersistenceObject.
struct Tab2: View {
@FetchRequest(entity: MyPersistenceObjectParentParent.entity(),
sortDescriptors: [])
private var parentParents: FetchedResults<MyPersistenceObjectParentParent>
}
I then have NavigationLinks to a Tab2Child View, which receive one MyPersistenceObjectParentParent instance. This child then inits another FetchRequest in the init with some attribute
struct Tab2Child: View {
@Environment(\.managedObjectContext) var managedObjectContext
@ObservedObject var parentParent: MyPersistenceObjectParentParent
@FetchRequest
var myPersistenceObjectParents: FetchedResults<MyPersistenceObjectParent>
init(parentParent: MyPersistenceObjectParentParent) {
self.parentParent = parentParent
self._myPersistenceObjectParents = FetchRequest(entity: MyPersistenceObjectParent.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "parentParent = %@", argumentArray: [parentParent]))
}
The view Tab2Child then has two further child views, which get passed one instance of MyPersistenceObjectParent.
The leaf child Detail-View(see above) then gets passed into the the persistent @ObservedObject MyPersistenceObject into the view, because the parent of the child-View uses a ForEach on one instance of the MyPersistenceObjectParent.
struct Detail-View2-Parent: View {
@Environment(\.managedObjectContext) var managedObjectContext
@ObservedObject
var myPersistenceObjectParent: MyPersistenceObjectParent
		var body: some View {
List {
ForEach(myPersistenceObjectParent.myPersistenceObjects, id: \.self) { myPersistenceObject in
DetailView(myPersistenceObject: myPersistenceObject)
}
Sorry, that its not really clearly structured. :(
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: