Database Reference
In-Depth Information
overall array. Once that's done, we look inside the
mainBundle
again for any
freestanding
mom
resources. Finally, we do a failure check to make sure we
have some models to look through. If we can't find any, we populate the
NSError
and return
NO
.
Finding the Mapping Model
Now the complicated part comes in. Since it is not currently possible to get
an
NSMappingModel
with just the source model and then determine the destina-
tion model, we have to instead loop through every model we find, instantiate
it, plug it in as a possible destination, and see whether there is a mapping
model in existence. If there isn't, we continue to the next one.
NSMappingModel *mappingModel = nil;
NSManagedObjectModel *targetModel = nil;
NSString *modelPath = nil;
for
(modelPath in modelPaths) {
targetModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
mappingModel = [NSMappingModel mappingModelFromBundles:nil
forSourceModel:sourceModel
destinationModel:targetModel];
//If we found a mapping model then proceed
if
(mappingModel)
break
;
//Release the target model and keep looking
[targetModel release], targetModel = nil;
}
//We have tested every model, if nil here we failed
if
(!mappingModel) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:@
"No models found in bundle"
forKey:NSLocalizedDescriptionKey];
*error = [NSError errorWithDomain:@
"Zarra"
code:8001
userInfo:dict];
return
NO;
}
This section is probably the most complicated piece of the progressive
migration routine. In this section, we're looping through all the models that
were previously discovered. For each of those models, we're instantiating the
model and then asking
NSMappingModel
for an instance that will map between
our known source model and the current model. If we find a mapping model,
we break from our loop and continue. Otherwise, we release the instantiated
model and continue the loop. After the loop, if the mapping model is still
nil
,
we generate an error stating that we cannot discover the progression between