Skip to content

Instantly share code, notes, and snippets.

@ryangittings
Last active March 1, 2021 23:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryangittings/938496f9414c0b9e075775a863987b25 to your computer and use it in GitHub Desktop.
Save ryangittings/938496f9414c0b9e075775a863987b25 to your computer and use it in GitHub Desktop.
import CoreData
import SwiftUI
struct ContentView : View {
var request: FetchRequest<Task>
init(taskID: Int) {
self.request = FetchRequest<Task>(fetchRequest: ContentViewTest.taskRequest(taskID: taskID))
}
var tasks: [Task] {
return request.wrappedValue.compactMap({ $0 })
}
var body: some View {
List {
ForEach(self.tasks, id: \.self) { task in
Text(String(task.id))
}
}
}
static func taskRequest(taskID: Int) -> NSFetchRequest<Task> {
let request: NSFetchRequest<Task> = Task.fetchRequest()
request.predicate = NSPredicate(format: "id == %d", taskID)
request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
return request
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment