title: Remove sqlite database and replace with default from bundle
body: <p>I used the Master-Detail-Application example at Xcode 4.3.3 to develope a data viewer. The app is downloading several files and append each file to a cell at the tableview. everything works fine. but if the user starts the download again, I need to remove all the previous data from the database and the tableview.
  I ended up with the following steps:</p>
  
  <ol>
  <li>remove the old store from coordinator</li>
  <li>remove the old sqlite database file</li>
  <li>copy the default ( empty ) sqlite db file from the bundle dir into the doc dir</li>
  <li>update store coordinator</li>
  <li>update tableview.</li>
  </ol>
  
  <p>but now the problem is, that if I do so, the new data is not written into sqlite file. 
  Please, can someone check my code. The following code is located at the MasterViewController.m ! I working on this since several days and I am totally stuck !!!</p>
  
  <p>Thank you!</p>
  
  <pre class="lang-objc prettyprint-override"><code>NSLog(@"Delete existing database file and copy default from bundle dir!");
  
  NSFileManager* fileManager = [[NSFileManager alloc] init];
  fileManager = [[NSFileManager alloc] init];
  
  NSError *error = nil;
  
  //get the documents directory:    
  
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  
  // create database path documents dir
  
  NSString *databasepath = [documentsDirectory stringByAppendingPathComponent:@"MyDatabase.sqlite"];
  NSLog(@"Destination Database Path = %@", databasepath);
  
  // create database path bundle dir
  
  NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDatabase.sqlite"];
  NSLog(@"Recource Database Path = %@", resourceDBFolderPath);
  
  // remove old store from coordinator
  error = nil;
  
  NSManagedObjectModel *managedObjectModel;
  
  NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDatabase" withExtension:@"momd"];
  managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
  
  // 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 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]];
  
  NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
  
  NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"MyDatabase.sqlite"];    
  
  if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&amp;error]) 
      if (error != nil){
  
          NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
      }    
  
  
  NSPersistentStore *persistentStore = [[persistentStoreCoordinator persistentStores] objectAtIndex:0];
  [persistentStoreCoordinator removePersistentStore:persistentStore error:&amp;error];
  if (error) 
      if (error != nil){
          NSLog(@"error removing persistent store %@ %@", error, [error userInfo]);
      }
  
  // Remove old database file
  
  [[NSFileManager defaultManager] removeItemAtPath: [documentsDirectory stringByAppendingPathComponent: @"MyDatabase.sqlite"] error: &amp;error];
  if (error != nil){
      NSLog(@"delete Database status: %@", [error localizedDescription]);
  }
  
  // copy default empty database file into documents dir
  
  [fileManager copyItemAtPath: resourceDBFolderPath toPath: databasepath           
                        error: &amp;error];
  if (error != nil){
      NSLog(@"copy default Database status: %@", [error localizedDescription]);
  } 
  
  
  // then update storecoordinator
  
  // add clean store file back by adding a new persistent store with the same store URL
  if (![persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType 
                                                configuration: nil 
                                                          URL: storeURL 
                                                      options: nil 
                                                        error: &amp;error]) {
      NSLog(@"failed to add db file, error %@, %@", error, [error userInfo]);
  }
  
  
  NSLog(@"Database replacement end!");        
  </code></pre>