// For this example, the name of my Core Data Entity is 'TaskItem'
// This can be placed in any SwiftUI View Struct to declare a variable array of your core data entries that automatically refreshes on changes to the core data model
@FetchRequest(
sortDescriptors: [
NSSortDescriptor(keyPath: TaskItem.dueTime, ascending: true),
NSSortDescriptor(keyPath: TaskItem.complete, ascending: true)
],
predicate: NSCompoundPredicate(andPredicateWithSubpredicates: [NSPredicate(format: "%K >= %@", #keyPath(TaskItem.dueTime), todayDateFrom! as NSDate)]),
animation: .default
) var tasks: FetchedResults<TaskItem>
override func viewWillAppear(_ animated: Bool) {
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
if let coreDataToDoItems = try? context.fetch(ToDoItem.fetchRequest()) as? [ToDoItem] {
toDo = coreDataToDoItems
}
}
}
// entire usable func for fetching data
func loadData() {
if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
if let hellos = try? context.fetch(Hello.fetchRequest()) as? [Hello] {
}
}
}