I'm doing a FetchRequest before the body of my page but can't add in the predicate as the id that I'm after isn't available until after self is available.
I've tried moving the query to a method triggered by a button press, but I get a segmentation fault.
Is there any way that I can add the predicate after the initial query to get my collection?
Alternatively, is there a way of passing in the id to the page direct? (Its a detail page, but its not triggered by selecting a list item)
If I hard code the id, everything works as expected, I just can't seem to get the id in the original query.
thanks
Your code is incomplete, so I cannot test, but can you try something like this:
struct EditSocialView: View {
@Environment(\.managedObjectContext) var managedObjectContext
@Environment(\.presentationMode) var presentationMode
var lodge: Lodge = getLodgeData()
@State var lodgeId = -1
var body: some View {
//...
EditSocialList(lodgeId: lodgeId)
.onAppear(perform: {
self.lodgeId = lodge.id
})
//...
}
}
struct EditSocialList: View {
var lodgeId: Int
//this gets the list
@FetchRequest var cdlodges: FetchedResults<CDLodge>
init(lodgeId: Int) {
self.lodgeId = lodgeId
self._cdlodges = FetchRequest(
entity: CDLodge.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),
],
predicate: NSPredicate(format: "lodgeid == \(lodgeId)")
)
}
var body: some View {
//use `cdlodges` here...
}
}
With separating the parts depending on FetchedResults, you may be able to initialize the FetchRequest using lodgeId
.