Search code examples
ioscore-datarestkit

RESTKit: Making minor changes to Persistent Store


If add an attribute to any of the entities, I have to delete and reinstall the App because it crashes. I was under the impression that the options below will allow to me make minor changes to the persistent store?

- (id)optionsForSqliteStore {
    return @{
             NSInferMappingModelAutomaticallyOption: @YES,
             NSMigratePersistentStoresAutomaticallyOption: @YES
             };
}

- (RKManagedObjectStore *)setupCoreDataWithRESTKit
{

    NSError * error;
    NSURL * modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"App" ofType:@"momd"]];
    NSManagedObjectModel * managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    self.managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];


    [self.managedObjectStore createPersistentStoreCoordinator];

    NSArray * searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * documentPath = [searchPaths objectAtIndex:0];
    NSPersistentStore * persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:[NSString stringWithFormat:@"/App.sqlite" fromSeedDatabaseAtPath:nil withConfiguration:nil options:[self optionsForSqliteStore] error:&error];
    NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

    NSLog(@"Path: %@", [NSString stringWithFormat:@"%@/App%@.sqlite", documentPath, [TBSPersistence username]]);

    if(!persistentStore){
        NSLog(@"Failed to add persistent store: %@", error);
    }

    [self.managedObjectStore createManagedObjectContexts];

    return self.managedObjectStore;

}

Here is the Log:

014-05-22 10:12:23.631 App[479:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed to add persistent store with error: Error Domain=NSCocoaErrorDomain Code=134130 "The operation couldn’t be completed. (Cocoa error 134130.)" UserInfo=0x15ea8ee0 {URL=file:///var/mobile/Applications/4BA83C9C-E488-4B3D-B6CC-2CC39CC41AC5/Documents/App.sqlite, metadata={
    NSPersistenceFrameworkVersion = 479;
    NSStoreModelVersionHashes =     {
      // Here listed are all the Entities...
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "12CC0291-F86F-43B6-A762-873B304C5E6F";
    "_NSAutoVacuumLevel" = 2;
}, reason=Can't find model for source store}'

Solution

  • That depends...

    You need to version the model so that the difference can be quantified and you need to make changes within the bounds of auto-migration (check the docs, but you can't remove, change data type, ...).