I haven't had a chance to really verify my theory in a real app, and so don't have a code example to share. What I tried is like below to tweak the model mapping so it matches the model versions:
guard let fileURL = Bundle.main.url(forResource: "Model", withExtension: "xcmappingmodel"),
let mappingModel = NSMappingModel(contentsOf: fileURL),
let newEntityMappings = mappingModel.entityMappings else {
return
}
for entityMapping in newEntityMappings {
guard let sourceEntityName = entityMapping.sourceEntityName,
let destinationEntityName = entityMapping.destinationEntityName else {
continue
}
entityMapping.sourceEntityVersionHash = sourceModel.entityVersionHashesByName[sourceEntityName]
entityMapping.destinationEntityVersionHash = destinationModel.entityVersionHashesByName[destinationEntityName]
}
mappingModel.entityMappings = newEntityMappings
do {
try migrationManager.migrateStore(from: sourceStoreURL,
sourceType: sourceStoreType,
options: nil,
with: mappingModel,
toDestinationURL: destinationStoreURL,
destinationType: destinationStoreType,
destinationOptions: nil
)
} catch let error {
print(error) // Error handling.
}
I don't have more specific comment on your following-up questions either, except that you don't need a managed object context to use NSMigrationManager
and run migrateStore(from:type:options:mapping:to:type:options:)
.
I guess this is the point where I'd suggest that you go ahead to play with the API. If you see anything strange related to the APIs and don't mind to share here, I may be able to take a closer look.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.