How to remove a core data persistent store
Asked Answered
R

3

6

I need to delete my persistent store (doing it object by object is not practical because I have over 100,000 objects). I've tried this:

- (IBAction)resetDatabase:(id)sender {

    NSPersistentStore* store = [[__persistentStoreCoordinator persistentStores] lastObject];

    NSError *error = nil;
    NSURL *storeURL = store.URL;

    // release context and model
    [__managedObjectContext release];
    [__managedObjectModel release];
    __managedObjectModel = nil;
    __managedObjectContext = nil;

    [__persistentStoreCoordinator removePersistentStore:store error:nil];

    [__persistentStoreCoordinator release];
    __persistentStoreCoordinator = nil;

    [[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
    if (error) {
        NSLog(@"filemanager error %@", error);
    }    

     // recreate the stack
     __managedObjectContext = [self managedObjectContext];

}

But I get this error when I try to insert entities into the store afterwards:

This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.

Update: I tried releasing the MOC and MOM before removing the persistent store but I still get the same error.

Recover answered 23/4, 2011 at 1:2 Comment(0)
S
9

Here is how I do a "reset data" function in several apps:

- (void)reset {
  // Release CoreData chain
  [_managedObjectContext release];
  _managedObjectContext = nil;
  [_managedObjectModel release];
  _managedObjectModel = nil;
  [_persistentStoreCoordinator release];
  _persistentStoreCoordinator = nil;

  // Delete the sqlite file
  NSError *error = nil;
  if ([fileManager fileExistsAtPath:_storeURL.path])
    [fileManager removeItemAtURL:_storeURL error:&error];
  // handle error...
}

Basically I just release the CoreData chain, then delete the persistentStore file. That's what you are trying to do, without using removePersistentStore, which I do not care since I will just rebuild the persistentStore coordinator later. Then at next core data call the chain is rebuilt transparently using singleton-lazy-style constructors like :

- (NSManagedObjectModel *) managedObjectModel {
  if (!_managedObjectModel)
    _managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
  return _managedObjectModel;
}
Steelhead answered 24/4, 2011 at 11:21 Comment(3)
Wow, I think the problem was removePersistentStore. Without that line it worked... I'm curious as to why though.Recover
I have a sense that just removing the .sqlite file will leave behind a lot of garbage when using core data attributes that "Allow External Storage".Tyrosine
magma is right, if you use iCloud is used etc problems will happen.Crider
E
2

You can do it externally given that you only need to do this while developing your application. I have a terminal open in which I remove the store manually before re-running my app. All you need to know is where it is located. I log it to console everytime my app runs with the following code:

[[CoreDataSingleton sharedManager] managedObjectContext]; //be sure to create the store first!

//Find targeted mom file in the Resources directory
NSString *momPath = [[NSBundle mainBundle] pathForResource:@"Parking" ofType:@"mom"];
NSLog(@"momd path: %@",momPath);

Hope that helps!

Earthly answered 23/4, 2011 at 1:39 Comment(0)
W
2

You need to make sure that any managed object context attached to the persistent store have been released before you try to delete the store. Otherwise, the context will evoke that error.

Winfield answered 23/4, 2011 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.