I have CoreData-based data layer (using a SQLite datastore) that I am using in both an iOS app and on the server that the iOS clients talk to. The data layer (objc code + coredata model / mapping defns) is compiled into the iOS bundle as per usual, and is compiled into a Framework bundle for use on OSX.
I am hitting a brick wall with default migration using mapping models.
On iOS, it works fine. The first time running the app in the simulator after adding the new datamodel verison, it migrates all the data when you call addPersistentStoreWithType:configuration:...
as per the standard Apple docs.
On OSX / PyObjC, it fails with Persistent store migration failed, missing mapping model
, i.e. for some reason the mapping model .cdm file can't be found in that bundle even though it is present.
If you manually specify the source / dest / mapping models by looking them up in the bundle and then manually invoke a migration through a NSMigrationManager, everything works fine, e.g.
bundle = objc.loadBundle( "MyApp_OSX", globals(),
os.path.join( base, FRAMEWORK_FILENAME ) )
# URLs of input and output datastores
datastoreURL = NSURL.fileURLWithPath_( datadir + "/MyApp.hsdb" )
outURL = NSURL.fileURLWithPath_( datadir + "/MyApp-migrated.hsdb" )
# URLs of old and new version MOMs and the mapping model
momd = bundle.URLForResource_withExtension_( "MyApp.momd", None )
url1 = momd.URLByAppendingPathComponent_( "MyApp 21.mom" )
url2 = momd.URLByAppendingPathComponent_( "MyApp 22.mom" )
mappingURL = bundle.URLForResource_withExtension_( "Test.cdm", None )
# Old and new MOMs and the mapping model
mom1 = NSManagedObjectModel.alloc().initWithContentsOfURL_( url1 )
mom2 = NSManagedObjectModel.alloc().initWithContentsOfURL_( url2 )
mm = NSMappingModel.alloc().initWithContentsOfURL_( mappingURL )
# Do the migration
migration = NSMigrationManager.alloc().initWithSourceModel_destinationModel_(
mom1, mom2 )
migration.migrateStoreFromURL_type_options_withMappingModel_toDestinationURL_destinationType_destinationOptions_error_(
datastoreURL, NSSQLiteStoreType, None, mm, outURL, NSSQLiteStoreType, None, None )
At this point I have no idea why the iOS version is able to find the mapping model to successfully migrate the datastore but the OSX / PyObjC version can't, despite clearly having the mapping model in the bundle, and the mapping model apparently being valid since it works when you invoke it manually.
Any insight into how CoreData searches for valid / appropriate mapping models in bundles that might help identify how to make this work on OSX would be much appreciated.