title: How to force coredata to rebuild sqlite database model?
body: <p>In my app I sometimes need to rebuild and repopulate database file. SQLite databse is created and managed by CoreData stack.</p>
  
  <p>What I'm trying to do is drop the file and then simply recreate persistentStoreCoordinator object.</p>
  
  <p>It works under simulator but not on device, where I'm getting such an error:</p>
  
  <pre><code>NSFilePath = "/var/mobile/Applications/936C6CC7-423A-46F4-ADC0-7184EAB0CADD/Documents/MYDB.sqlite";
  NSUnderlyingException = I/O error for database at /var/mobile/Applications/936C6CC7-423A-46F4-ADC0-7184EAB0CADD/Documents/MYDB.sqlite.  SQLite error code:1, 'table ZXXXX already exists';
  </code></pre>
  
  <p>I cannot find the cause of this in any way. It indicates two different problems - Cocoa error 256 indicates that file does not exist or is not readable. But file <strong>IS</strong> created after creating persistenStoreCoordinator, although it's empty, but after executing some queries it disappears.</p>
  
  <p>Second message indicating attempt to create alredy existing table is quite strange in that case.</p>
  
  <p>I'm quite confused and cannot get the point what's going on here. My code looks like this:</p>
  
  <pre><code>NSString *path = [[WLLocalService dataStorePath] relativePath];
  NSError *error = nil;
  
  WLLOG(@"About to remove file %@", path);
  
  [[NSFileManager defaultManager] removeItemAtPath: path error: &amp;error];
  
  if (error != nil) {
   WLLOG(@"Error removing the DB: %@", error);
  }
  
  
  [self persistentStoreCoordinator];
  
  WLLOG(@"Rebuild DB result %d", [[NSFileManager defaultManager] fileExistsAtPath: path]);
  </code></pre>
  
  <p>After this code is exectued, DB file exists but is empty. When then first query (and all following) is executed, it gives me the error above and file disappears.</p>
  
  <p>Does anybody has an idea what's wrong with it?</p>
  
  <p>Big thanks for pointing me the right way!</p>