title: Reset a CoreData persistent store
body: <p>Basically, what I'm trying to do is to wipe out all data in my CoreData persistent store, then import new data. How would you do this? It seems that the simplest solution is call <code>[NSPersistentStoreCoordinator removePersistentStore:error:]</code> and then remove the file. Is that the best practice available? Is it thread-safe?</p>
  
  <p>Thank you very much,</p>
  
  <h2>#</h2>
  
  <p><strong>Question 0.1: was</strong></p>
  
  <p>I am trying to update the data in a CoreData persistent store. My user is seeing a table view with statistical data. I want to update the application by deleting all existing data, then importing new data. I would like to show a progress view to tell the user that the application is not hanging.</p>
  
  <p>I have added the following <code>resetPersistentStore</code> method in my AppDelegate (<code>persistentStoreCoordinator</code>is given for reference):</p>
  
  <pre><code>// ...
  @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
  @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
  @property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
  // ...
  
  /**
   Returns the persistent store coordinator for the application.
   If the coordinator doesn't already exist, it is created and the application's store added to it.
   */
  - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
  
      if (persistentStoreCoordinator != nil) {
          return persistentStoreCoordinator;
      }
  
      NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: kPersistentStoreFilename]];
  
      NSError *error = nil;
      persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
      if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      }    
  
      return persistentStoreCoordinator;
  }
  
  /**
   * Will remove the persistent store
   */
  - (NSPersistentStoreCoordinator *)resetPersistentStore {
      NSError *error;
  
      [managedObjectContext lock];
  
      // FIXME: dirty. If there are many stores...
      NSPersistentStore *store = [[persistentStoreCoordinator persistentStores] objectAtIndex:0];
  
      if (![persistentStoreCoordinator removePersistentStore:store error:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      }  
  
      // Delete file
      if (![[NSFileManager defaultManager] removeItemAtPath:store.URL.path error:&amp;error]) {
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
          abort();
      } 
  
      // Delete the reference to non-existing store
      [persistentStoreCoordinator release];
      persistentStoreCoordinator = nil;
  
      NSPersistentStoreCoordinator *r = [self persistentStoreCoordinator];
      [managedObjectContext unlock];
  
      return r;
  }
  </code></pre>
  
  <p>Then in my view I do (in another thread since I am using <code>MBProgressHUD</code>:</p>
  
  <pre><code>PatrimoineAppDelegate *appDelegate = (PatrimoineAppDelegate *)[[UIApplication sharedApplication] delegate]; 
  // Delete everything
  [appDelegate resetPersistentStore];
  </code></pre>
  
  <p>And I get an <code>EXC_BAD_ACESS</code>...</p>
  
  <p>I do not know CoreData or multithreading very well, maybe I'm doing an evident error...</p>