Created
November 23, 2019 04:01
-
-
Save acwright/827e527e47099455cc54afd18679ae56 to your computer and use it in GitHub Desktop.
Fetch Request with dynamic predicate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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