Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting all data in a Core Data entity in Swift 3

Is there a way to do a batch delete of all data stored in all of the entities in core data?

I read somewhere that in iOS 9 or 10 that apple introduced a way to do batch deletes, but I can't seem to find any good information on it.

Ultimately, I just need a function that goes through an entity, and deletes all of the data in it. Seems like it should be simple enough, but documentation/tutorials for it have proven exceedingly difficult to find.

Any thoughts?

Edit

I added the following code into an IBAction attached to a button:

@IBAction func clearAllData(_ sender: AnyObject) {     let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "PLProjects")     let request = NSBatchDeleteRequest(fetchRequest: fetch)      //get the data from core data     getPLData()      //reload the table view     tableView.reloadData() } 

This does not seem to work however. If I close down the project and reopen it, the data is still there. I am assuming this is also why the table view doesn't update, because the data is not actually being deleted.

like image 540
John Hubler Avatar asked Oct 07 '16 15:10

John Hubler


People also ask

How do I delete all entries in Core Data?

One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.

How do I delete an object in Core Data?

Table views have a built-in swipe to delete mechanic that we can draw upon to let users delete commits in our app. Helpfully, managed object context has a matching delete() method that will delete any object regardless of its type or location in the object graph.

What is delete rule in Core Data?

A delete rule defines what happens when the record that owns the relationship is deleted. Select the notes relationship of the Category entity and open the Data Model Inspector on the right. By default, the delete rule of a relationship is set to nullify. Core Data supports four delete rules: No Action.


2 Answers

You're thinking of NSBatchDeleteRequest, which was added in iOS 9. Create one like this:

let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Employee") let request = NSBatchDeleteRequest(fetchRequest: fetch) 

You can also add a predicate if you only wanted to delete instances that match the predicate. To run the request:

let result = try managedObjectContext.executeRequest(request) 

Note that batch requests don't update any of your current app state. If you have managed objects in memory that would be affected by the delete, you need to stop using them immediately.

like image 184
Tom Harrington Avatar answered Oct 13 '22 02:10

Tom Harrington


To flesh out Tom's reply, this is what I added to have a complete routine:

func deleteAllRecords() {     let delegate = UIApplication.shared.delegate as! AppDelegate     let context = delegate.persistentContainer.viewContext      let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "CurrentCourse")     let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)      do {         try context.execute(deleteRequest)         try context.save()     } catch {         print ("There was an error")     } } 
like image 35
Zonker.in.Geneva Avatar answered Oct 13 '22 01:10

Zonker.in.Geneva