2
votes

I am attempting to create an iOS Today Extension. I would like to connect to the main apps CoreData SQL DB. However i am receiving this error when i attempt to connect.

"The model used to open the store is incompatible with the one used to create the store} with userInfo dictionary"

Accessing Core Data SQL Database in iOS 8 Extension

The creation of the Database happens and I am able to insert records etc. My extension controller code now is using similar code to the CoreData code in the app delegate.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    // -- Changed for Today Screen --//
    //NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
    //                                         stringByAppendingPathComponent: @"CoreDB_2014.sqlite"]];
    NSURL *storeUrl = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.THISAPP.APPNAME"];
    NSLog(@"StoreURL1: %@", storeUrl);
    storeUrl = [storeUrl URLByAppendingPathComponent:@"CoreDB_2014A.sqlite"];
    NSLog(@"StoreURL2: %@", storeUrl);
    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
    }
    return persistentStoreCoordinator;
}

Any help would be appreciated.

Thanks

1

1 Answers

3
votes

That error is pretty self-explanatory: it means that you changed the Core Data model so that it no longer matches the one used to create the persistent store file that you're trying to open. Those have to match. If you change the model, you need to either (a) use more than one model version and perform a migration to the new model, or (b) use a different persistent store file (or delete the existing one and start over).

Core Data models often change while an app is being developed. In most cases, for a pre-release app, the developer will use option (b) and delete previous test data. If that's not feasible, you'll need to do model versioning and migration to update the data store in place.