4
votes

I have seen this question posted here, but always, the answer is that the model has been changed, reset the simulator, remove the store.

I am also getting this error, however, this is a new app. I have not added/changed entities nor attributes.

I've deleted the store, I've reset the simulator, but I get the same results.

Here is the store code.. Is there any other cause for this problem?

- (NSManagedObjectModel *)managedObjectModel {
   //  NSLog(@"%s", __FUNCTION__);
    if (managedObjectModel_ != nil) {
        return managedObjectModel_;
    }

    NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"Lexicon" ofType:@"momd"];
    NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
    managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return managedObjectModel_;
}

/**
 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: @"dict.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

Here is the console spew:

2011-08-03 11:42:38.936 Lexicon[4468:f203] -[AppDelegate application:didFinishLaunchingWithOptions:] 2011-08-03 11:42:38.989 Lexicon[4468:f203] -[AppDelegate applicationDocumentsDirectory] 2011-08-03 11:42:39.048 Lexicon[4468:f203] Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x6d3ee60 {metadata={type = immutable dict, count = 7, entries => 2 : {contents = "NSStoreModelVersionIdentifiers"} = {type = immutable, count = 1, values = ( 0 : {length = 0, capacity = 0, bytes = 0x} )} 4 : {contents = "NSPersistenceFrameworkVersion"} = {value = +363, type = kCFNumberSInt64Type} 6 : {contents = "NSStoreModelVersionHashes"} = {type = immutable dict, count = 1, entries => 0 : {contents = "LexiconEntity"} = {length = 32, capacity = 32, bytes = 0x8698c5295fa5124b78a6b127bba26ff0 ... 70eaece0517cd4c6} }

7 : {contents = "NSStoreUUID"} = {contents = "86B22D58-28A5-4585-8650-07111B34B43A"} 8 : {contents = "NSStoreType"} = {contents = "SQLite"} 9 : {contents = "_NSAutoVacuumLevel"} = {contents = "2"} 10 : {contents = "NSStoreModelVersionHashesVersion"} = {value = +3, type = kCFNumberSInt32Type} } , reason=The model used to open the store is incompatible with the one used to create the store}, { metadata = { NSPersistenceFrameworkVersion = 363; NSStoreModelVersionHashes = { LexiconEntity = <8698c529 5fa5124b 78a6b127 bba26ff0 f3bc678b a4f1e809 70eaece0 517cd4c6>; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( <> ); NSStoreType = SQLite; NSStoreUUID = "86B22D58-28A5-4585-8650-07111B34B43A"; "_NSAutoVacuumLevel" = 2; }; reason = "The model used to open the store is incompatible with the one used to create the store"; }

1

1 Answers

2
votes

It turns out that the sqlite DB store was built incorrectly. So, in this case, the message was rather accurate.

The issue above was real. The database did not reflect the model.

The solution is to let Core Data create the empty data base from the model, and then have Core Data import the data itself.

I first exported my sqlite database to sql calling the file db.sql (imaginative!). I only exported the data table and the primary key table, not the metadata table. I used an app called SQLiteManager for this. You can also do it at the command line.

All the code is stock stuff except for handling the Persistent Store Controller..

That code is as follows:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }


    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"db.sql"];

    // set up the backing store
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
        }
    }

    NSURL *storeURL = [NSURL fileURLWithPath:storePath];

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

I hope this helps.. It seems to work for me..