Skip to content

Instantly share code, notes, and snippets.

@acwright
Created November 23, 2019 04:01
Show Gist options
  • Select an option

  • Save acwright/827e527e47099455cc54afd18679ae56 to your computer and use it in GitHub Desktop.

Select an option

Save acwright/827e527e47099455cc54afd18679ae56 to your computer and use it in GitHub Desktop.
Fetch Request with dynamic predicate
import SwiftUI
struct StudentsView: View {
var request: FetchRequest<Student>
var students: FetchedResults<Student>{ request.wrappedValue }
// MARK: - Lifecycle
init(
predicate: NSPredicate = NSPredicate(value: true),
sortDescriptors: [NSSortDescriptor] = []
) {
self.request = FetchRequest(
entity: Student.entity(),
sortDescriptors: sortDescriptors,
predicate: predicate
)
}
// MARK: - Body
var body: some View {
List {
ForEach(students) { student in
Text(student.name ?? "No Name")
}
}
}
}
struct StudentsView_Previews: PreviewProvider {
static var previews: some View {
StudentsView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment