Is there any sense in performing background thread for DELETING OBJECTS? The reset button freezes but that's due to the animation, so I just stick with:
self.companies.removeAll()
self.tableView.reloadData()
Then it works instantly. But would you implement delete in background or that's not necessary?
Even if I try to delete them in background queue it gives: 'An NSManagedObjectContext cannot delete objects in other contexts', made an error somewhere:
@objc private func handleReset() {
CoreDataManager.shared.persistentContainer.performBackgroundTask { (backgroundContext) in
self.companies.forEach { (company) in
backgroundContext.delete(company)
}
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: Company.fetchRequest())
do {
try backgroundContext.execute(batchDeleteRequest)
self.companies.removeAll()
self.tableView.reloadData()
} catch let deleteError {
print("Failed to delete objects from Core Data", deleteError)
}
}
}