"Can't Find Model for Source Store" Occurring During iPhone "Automatic Lightweight Migration"

Can't find model for source store occurring during iphone Automatic Lightweight Migration?

You set the version of your data model in the Utilities inspector (right hand pane), under the Identity and Type tab when the xcdatamodeld file is selected. This has a section called "Core Data Model", and a field called "Identifier".

You add a new model version by selecting the xcdatamodeld file, the going to Editor --> Add model version.

At this point it prompts you for the previous model to base it on.

If youve added a new model without going through this process the lightweight migration may not work.

iPhone CoreData migration fails with Can't find model for source store

From the linked answer,

This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.

It sounds like you made version 2, edited version 2, ran the app, edited version 2 again, and ran the app again. This doesn't work so well; you need to save all the model versions you expect to be able to open. This is a bit of a hassle.

What you could do is name all your models after app versions, e.g. FooModel-1 and FooModel-1.1 corresponding to releases, and FooModel-1.2d1, FooModel-1.2d2 for "development" versions. Before release, you can rename FooModel-1.2d10 to FooModel-1.2 and remove the other development versions.

(Or I could be entirely misreading the question; sorry.)

iPhone Core Data Lightweight Migration Cocoa error 134130: Can't find model for source store

Folks,

For the record, I ceased use of core data completely. My code was complicated and, per the issues above, unreliable. I use SQLLite directly now and am WAY happier. I cannot recommend using Core Data in any scenario.

Implementation of Automatic Lightweight Migration for Core Data (iPhone)

This is what I did to make Automatic Lightweight Migration (Source: http://brainwashinc.wordpress.com/2010/01/18/iphone-coredata-automatic-light-migration/)

1. Set the Persistent Store options for automatic migration in the app delegate.

Change your persistentStoreCoordinator creation to this (replace YOURDB):

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

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

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"YOURDB.sqlite"]];

// handle db upgrade
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
}

return persistentStoreCoordinator;
}

2. Version your Data Model and Edit the new file.

Select your xcdatamodel file
Design -> Data Model -> Add Model Version (expand your xcdatamodeld item)
Select the “2″ (or later) file, Design -> Data Model -> Set Current Version (edit this version)

3. Specify the momd resource in app delegate.

Change your managedObjectModel implementation to this (replace YOURDB)

- (NSManagedObjectModel *)managedObjectModel {

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

NSString *path = [[NSBundle mainBundle] pathForResource:@"YOURDB" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

return managedObjectModel;
}

CoreData lightweight migration crashes, even after adding a mapping model

The short version is that automatic lightweight migration can't make this change, even with a mapping model. You need to do a full manual migration to make this change. If you check out Apple's lightweight migration documentation, it says that

...if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination.

This is what you're trying to do, but it can't happen automatically.

The longer answer to why gets into some details of how Core Data works. Xcode doesn't do a good job of making this obvious.

  • When you have two or more entities without a common parent entity, Core Data treats them as completely independent. Your SQLite has a Book table, an Author table, and so on.
  • When you have two or more entities with a common parent, Core Data treats them as special cases of the same table. Your SQLite has an IDManagedObject table. Core Data uses other logic to decide if an entry in that table is a Book or an Author or something else.

Because of this, what you're actually doing is merging more than one entity into a single new entity. Core Data doesn't know how to do that automatically, which is what it's telling you in that error message.

Core Data is only complaining about those two tables because they're the first ones it sees. It can't handle merging any of them, and it only mentions two tables because it's giving up before looking at the rest.

To make this change, you'll need to create a migration manager and write code to do the migration. Apple describes this in Customizing the Migration Process. Or, depending on what exactly you need, there might be other solutions that don't require adding a new parent entity.

Core Data Migration - Can't find mapping model for migration

Turns out the Mapping Model needs to be part of the .xcdatamodeld package, which is impossible to do within xcode 4. With it in there, it works great.

Note: my mapping file stopped working again, and I found that it was 0KB on disc, so I had to recreate it again and it worked fine after that. I quickly committed it and will see if it disappears again. The problem is that Xcode 4 indexes it or something so it looks fine in xcode but on the file system its empty.

See my comment below for the reason (xcode 4's mapc (map compiler) is broken).

** THIS HAS BEEN FIXED IN THE NEWER XCODEs/SDKs.**



Related Topics



Leave a reply



Submit