Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data: How to handle new versions?
    text
    copied!<p>I've released an app with a Core Data sqlite database. In the new version of my app, I've created a new "Model Version" of my <code>xcdatamodel</code> in XCode. In the new version an entity is removed and some new attributes are added to one of the entities.</p> <p>When updating to the new app version, I'm getting this sql error:</p> <blockquote> <p>The model used to open the store is incompatible with the one used to create the store</p> </blockquote> <p>How can I handle this error? All data in the database are downloaded from the web, so maybe the easiest way is to just delete the current sqlite file when this error occurs and start from scratch -- but what do people do when the database contains data that cannot be regenerated?</p> <p><strong>SOLUTION:</strong></p> <p>I've created an Mapping Model in Xcode and changed my persistentStoreCoordinator getter to handle an option dictionary to the <code>addPersistentStoreWithType:configuration:URL:options:error:</code> method with the key <code>NSMigratePersistentStoresAutomaticallyOption</code>.</p> <pre><code>- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } NSURL *cacheURL = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; NSURL *storeURL = [cacheURL URLByAppendingPathComponent:@"MyDatabase.sqlite"]; NSString *storePath = [storeURL path]; NSError *error = nil; NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return __persistentStoreCoordinator; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload