如何访问妈妈包中的妈妈文件?

时间:2012-01-17 18:47:22

标签: ios core-data core-data-migration

我需要你的帮助, 我正在学习coreData迁移,我无法访问my momd包中的两个模型文件。

我做到了这一点:

在两种模型的init方法中:

urlModel2 =[[NSBundle mainBundle] URLForResource:@"DeptB" withExtension:@"mom"];

另一种方法是:

NSManagedObjectModel* model2 = [[NSManagedObjectModel alloc] initWithContentsOfURL:[self urlModel2]];

我想让每个NSManagedObjectModel中的两个模型进行验证:

    NSMappingModel *mappingModel =
    [NSMappingModel inferredMappingModelForSourceModel:[self sourceModel]
                    destinationModel:[self destinationModel] error:outError];

您可以在http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html

找到此信息

提前感谢!

1 个答案:

答案 0 :(得分:2)

我找到了解决方案:

NSString* pathMOMD = [[NSBundle mainBundle] pathForResource:@"Dept" ofType:@"momd"];
NSBundle* bundleMOMD = [NSBundle bundleWithPath:pathMOMD];

// model 1
NSString* pathMOM = [bundleMOMD pathForResource:@"DeptB" ofType:@"mom"];
urlModel1 = [NSURL fileURLWithPath:pathMOM];
NSManagedObjectModel* model1 = [[NSManagedObjectModel alloc] initWithContentsOfURL:[self urlModel1]];
if(!model1){ NSLog(@"Problem for Model1"); }else{ NSLog(@"ok 1");}