views:

122

answers:

1

I would like to write a method that deletes my application's Core Data store, which I would follow by creating a new store. Is there a standard approach for doing this, and where is the database located in my application's sandbox?

+2  A: 

You (perhaps unwittingly) have already told it where to go. In Apple's Template code, the ApplicationDelegate automatically creates this store in the Application's Documents folder. Take a look at the -(NSPersistentStoreCoordinator *) persistentStoreCoordinator method in your ApplicationDelegate.

Matt B.
It sounds like it's enough to delete the file, set `persistentStoreCoordinator` to `nil` and run the app delegate's `persistentStoreCoordinator` method. Does that sound about right?
Alex Reynolds
I've tried doing exactly that, and it doesn't quite work. By releasing your `persistentStoreCoordinator`, you invalidate the Managed Object Context's link. And the context doesn't take kindly to accepting a new coordinator. You could create a whole new context, but then all those connections break. I'm sure there's a way to do it, but I gave up after futzing for a while and simply iterated through all my entities, deleting each one.
Matt B.
I don't think I can iterate through everything, since there can be many thousands of records. Anyone else have any thoughts?
Alex Reynolds
One thing I didn't try that might be worth a shot: Remove the persistent store (with `removePersistentStore:error:`) and then delete the file, and then add the persistent store back again. See: http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-core-data/1222145#1222145
Matt B.