darksky
darksky

Reputation: 21019

Clearing out Core Data

I am referring to the following question: Delete/Reset all entries in Core Data?

Sometimes upon startup, I would like to clear out my Core-Data database and re-download it all over again. I am just starting to use Core Data so I am a beginner in Core Data.

1) The answers shows:

NSPersistentStore *store = ...;

How would I get the NSPersistentStore? My App Delegate does not refer to the NSPersistentStore, only to the NSStorePersistentCoordinator.

2) Am I supposed to rebuild the stack after I run the following (in the answer)?

NSPersistentStore *store = ...;
NSError *error;
NSURL *storeURL = store.URL;
NSPersistentStoreCoordinator *storeCoordinator = ...;
[storeCoordinator removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];

If so, how do I rebuild the stack? Can I just call one function to do that?

Thanks,

Upvotes: 1

Views: 637

Answers (1)

Wasim
Wasim

Reputation: 1349

Why you want to use NSPersistentStore ?? Just because of that post ? If yes then i recommend you to go with `NSManagedObject. You can simply delete all your core data through a fetch request and then have a for loop on it. Its old approch but its easy to understand for beginners. This might help you:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = 
[NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];

NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];


for (NSManagedObject *managedObject in items) {
    [managedObjectContext deleteObject:managedObject];

}
if (![managedObjectContext save:&error]) {

}

Happy Coding..!

Upvotes: 1

Related Questions