I have View with a list of records from the CoreData entity and there is the following FetchRequest declared in the View
@FetchRequest(entity: Locations.entity(), sortDescriptors: [NSSortDescriptor(key: "locTimestamp", ascending: false)])
var locations: FetchedResults<Locations>
I have two functions for the deletion, one to delete just one record with the given ID
func deleteLocation(forID id: UUID) {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Locations")
request.predicate = NSPredicate(format: "locID == %@", id as CVarArg)
do {
let location = try myContext.fetch(request) as! [NSManagedObject]
myContext.delete(location.first!)
try myContext.save()
} catch let error {
errTitle = "Error"
errMessage = error.localizedDescription
isError = true
}
}
which works fine so List with records in View is refreshed and the second one for deletion of all records
func deleteAllLocations() {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Locations")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: request)
do {
try myContext.execute(deleteRequest)
try myContext.save()
} catch let error {
errTitle = "Error"
errMessage = error.localizedDescription
isError = true
}
}
which is executed (records deleted from CoreData entity) but List rows in View are not refreshed. What might be the issue here?