我有基于 CoreData 的数据层(使用 SQLite 数据存储),我在 iOS 应用程序和 iOS 客户端与之通信的服务器上都使用它。数据层(objc 代码 + coredata 模型/映射定义)按照惯例编译到 iOS 包中,并编译到框架包中以在 OSX 上使用。
我正在使用映射模型进行默认迁移。
在 iOS 上,它运行良好。添加新数据模型版本后第一次在模拟器中运行应用程序时,它会在您addPersistentStoreWithType:configuration:...
按照标准 Apple 文档调用时迁移所有数据。
在 OSX/PyObjC 上,它以 . 失败Persistent store migration failed, missing mapping model
,即由于某种原因,即使存在该包,也无法在该包中找到映射模型 .cdm 文件。
如果您通过在包中查找它们来手动指定源/目标/映射模型,然后通过 NSMigrationManager 手动调用迁移,则一切正常,例如
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 )
在这一点上,我不知道为什么 iOS 版本能够找到映射模型以成功迁移数据存储,但 OSX / PyObjC 版本却不能,尽管包中明确包含映射模型,并且映射模型显然是有效的因为它在您手动调用它时起作用。
任何有关 CoreData 如何在捆绑包中搜索有效/适当的映射模型的见解,这可能有助于确定如何在 OSX 上进行这项工作,我们将不胜感激。