Apologies
You must be signed in to watch this lesson.
Something that is very useful during development is the ability to wipe out all of your existing Core Data objects with a press of a button. In today's lesson we'll implement an easy way to reset Core Data by using a Batch Delete Request. In addition to this, I'll show you how to draw a footer view that describes to the user when there is no data in your list.

Comments (8)
mcordero
6 years ago
Hi Brian, how are we supposed to handle stale data? A few lessons back I was having problems with the edit feature because according to the console logs, CoreData makes a snapshot of the data you currently have in the context..? You end that session and then you re-run the app and try to edit that data from the last session; you would get a NSInvalidArgumentException when trying to call [Company -setName]. The only cure for this has seemed to be the Batch Delete Request, where every time I run I get a fresh database, with no data rows. A deeper explanation of this would really help. Thanks.
Brian Voong
6 years ago
mcordero
6 years ago
How are we supposed to refresh the UI? Correct, I am trying to update an object, that was created on the last compile, build, run session. So the workflow is: 1. Run app in Xcode, with iPhone X sim. 2. Create 1-3 companies 3. Stop app in Xcode. 4. Run app again. 1-3 companies comes back. 5. Try to edit a company. No data is loaded. 6. Enter new data. 7. Hit save. exception thrown @ CreateCompanyViewController:: line 111 source code: https://github.com/michaelcordero/IntermediateTrainingCoreData
Brian Voong
6 years ago
mcordero
6 years ago
awesome! thanks brian!
johnrm9
6 years ago
companies.enumerated().forEach{ (index, _) in let indexPath = IndexPath(row: index, section: 0) indexPathsToRemove.append(indexPath) } works just like the for loop: for (index, _) in companies.enumerated() { let indexPath = IndexPath(row: index, section: 0) indexPathsToRemove.append(indexPath) }
Brian Voong
6 years ago
johnrm9
6 years ago
Here's another way ... for index in companies.indices { let indexPath = IndexPath(row: index, section: 0) indexPathsToRemove.append(indexPath) } works just like the for loop. also: for (index, _) in companies.enumerated() { let indexPath = IndexPath(row: index, section: 0) indexPathsToRemove.append(indexPath) }
Brian Voong
6 years ago
Alan Swinburne
6 years ago
Hi Brian: Great Course: Because the RESET button deletes a lot of data, I was expecting to see some form of ALERT BOX to warn the user that data is about to be deleted? Message boxes are easy in VB ...XCODE ? What is "Stale Data" please.
arroyot24
3 years ago
Something this kind could be useful for you: let alert = UIAlertController(title: "¬°Atention!", message: "Descriptive text", preferredStyle: .actionSheet) let action = UIAlertAction(title: "Go on", style: .destructive) {_ in deleteData() let alertEnd = UIAlertController(title: "Deletion completed", message: "Descriptive text", preferredStyle: .actionSheet) let action = UIAlertAction(title: "OK", style: .default) {_ in ...action after resetting data, if so} alertEnd.addAction(action) self.present(alertEnd, animated: true, completion: nil) } alert.addAction(action) alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in alert.dismiss(animated: true, completion: nil) })) self.present(alert, animated: true, completion: nil)
tonyl
6 years ago
Hey Brian, I you don't mind I have a question, At 15:28 your Xcode gave you a beautiful error report, I was wondering if you edited the AppDelegate or an Xcode setting so that it gave you this error report? I have googled this question to death and have not found a way to make Xcode give me such a great crash report, my error report is a simple "*** Assertion failure", Thanks
Brian Voong
6 years ago
tonyl
6 years ago
Thanks for your hard work!
iHobbit
6 years ago
Why don't we have to execute a context.save() after the batch delete request?
iHobbit
6 years ago
Ahh, I see, the batch delete executes on the container rather than the context. Thanks.
Brian Voong
6 years ago
Paul Dong
6 years ago
Hi Brain, Could you please make the Comments area better, such as Markdown support? In Swift 4, to get the request for batchDeleteRequest object, have to make the type of request specific to NSFetchRequestResult: ``` let context = CoreDataManager.shared.persistenContainer.viewContext //have to make type specific, otherwise, it will get Ambiguous use of 'fetchRequest()' let request: NSFetchRequest<NSFetchRequestResult> = Company.fetchRequest() let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: request) ```
st4n
6 years ago
Hey Brian. I'm making a simple app with coredata to practice what i have learned here and I'm also using batchDeleteRequest. Unfortunately after wiping the data when I'm trying to add new I'm getting an error: NSMergeConflict for NSManagedObject with oldVersion and newVersion and old object snapshot. I'm adding new data from a different controller than the one I was removing from. Any ideas what to do? Plz help :)
Cinquain
5 years ago
Fire as usual!
HELP & SUPPORT