Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.
Edit
I'm looking to do this programmatically so that a user can essentially hit a reset
button.
Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank.
Edit
I'm looking to do this programmatically so that a user can essentially hit a reset
button.
Here is a somewhat simplified version with less calls to AppDelegate self and the last bit of code that was left out of the top rated answer. Also I was getting an error "Object's persistent store is not reachable from this NSManagedObjectContext's coordinator" so just needed to add that back.
iOS 10 and Swift 3
Assuming that your entity name is "Photo", and that you create a CoreDataStack class...
Here is a good tutorial of how to use CoreData and how to use this method. https://medium.com/compileswift/parsing-json-response-and-save-it-in-coredata-step-by-step-fb58fc6ce16f#.1tu6kt8qb
I remove all data from core data on a button Event in a HomeViewController class: This article helped me so much I figured I'd contribute.
Note that in order to call self.persistentStoreCoordinator I declared a property in the Home View Controller. (Don't worry about the managedObjectContext that I use for saving and loading.)
Then in the AppDelegate ApplicationDidFinishLaunching right below creating a HomeViewController I have :
I took Grouchal's code and to speed it up I used enumeration with concurrent mode (
NSEnumerationConcurrent
), it got a bit faster compared to for loop (in my app I added this feature for Testers so that they can clear data and do testcases rather than delete and install app)I've written a
clearStores
method that goes through every store and delete it both from the coordinator and the filesystem (error handling left aside):This method is inside a
coreDataHelper
class that takes care of (among other things) creating the persistentStore when it's nil.iOS 10 + Swift 3 solution:
Iterates through all of the core data entities and clears them