0

I tried to use the value of AppStore in CoreData's predicate.

struct MyView: View {
    @AppStorage("valueFilter") private var valueFilter = ""
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(
        entity: Value.entity(),
        sortDescriptors: [],
        predicate: NSPredicate(format: "value_filter == %@", valueFilter) // report error in this line.
    ) var values: FetchedResults<Value>
    ...
}

But it report Cannot use instance member 'valueFilter' within property initializer; property initializers run before 'self' is available in predicate line.

ccd
  • 5,788
  • 10
  • 46
  • 96
  • You can use the same approach as in https://stackoverflow.com/a/59345830/12299030. Prepare predicate in first view and move it in constructor of second view, which is created in body, so all properties are already available. – Asperi Aug 21 '20 at 08:37
  • The same example I think might serve more detail https://www.hackingwithswift.com/books/ios-swiftui/dynamically-filtering-fetchrequest-with-swiftui – ccd Aug 21 '20 at 10:17

0 Answers0