CoreData 深入理解6 Mapping Model
2016-03-09 16:59
183 查看
通常,我们都会尽量使数据模型的变化尽量简单。但有些情况下,不得不进行大的改动,甚至是重新设计数据模型。在这种情况下,之前提过的简单数据迁移已经无法适应了,需要引入Mapping Model这个中间层。
这时,又想起之前提过的一句话:
There is no problem in computer science that can’t be solved by adding another
level of indirection.
这里做一个简单的变动,先为球员增加薪水属性:
![](http://img.my.csdn.net/uploads/201301/22/1358822033_8468.png)
然后创建一名球员,信息如下:
![](http://img.my.csdn.net/uploads/201301/22/1358822051_2261.png)
这时候我们打算为球员调薪,比如上涨10%。为了结合NSMappingModel,这里简单地增加了一个新的属性newSalary,并且希望在数据迁移时更新该属性。为此,我们创建了一个NSMappingModel映射模型:
![](http://img.my.csdn.net/uploads/201301/22/1358822104_9477.png)
![](http://img.my.csdn.net/uploads/201301/22/1358822120_3729.png)
![](http://img.my.csdn.net/uploads/201301/22/1358822129_9552.png)
选择好源数据模型和目标数据模型,设置newSalary和salary的关系:
![](http://img.my.csdn.net/uploads/201301/22/1358822155_1229.png)
这表示目标属性newSalary的值为源属性salary的1.1倍。
这个时候,我们不希望Core Data自动为我们映射模型,所以修改一下迁移选项:
[cpp] view
plaincopy
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];
把NSInferMappingModelAutomaticallyOption设置为NO后,我们需要手工指定映射模型:
[cpp] view
plaincopy
NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"];
NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];
NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];
接着,进行实质性的数据迁移。简单起见,这里就没有做错误检查了:
[cpp] view
plaincopy
bf06
NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
if (![migrationManager migrateStoreFromURL:storeURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel toDestinationURL:tmpStoreURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {
NSLog(@"Error migrating %@, %@", error, [error userInfo]);
abort();
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *oldStoreName = @"cdNBA_old.sqlite";
NSURL *oldStoreURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:oldStoreName]];
[fileManager moveItemAtURL:storeURL toURL:oldStoreURL error:&error];
[fileManager moveItemAtURL:tmpStoreURL toURL:storeURL error:&error];
再跑一遍Demo,然后在终端里查看:
![](http://img.my.csdn.net/uploads/201301/22/1358822268_7261.png)
可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值:
![](http://img.my.csdn.net/uploads/201301/22/1358822296_1468.png)
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
Brief Talk About Core Data Series, Part 9 : Using Mapping Model
这时,又想起之前提过的一句话:
There is no problem in computer science that can’t be solved by adding another
level of indirection.
这里做一个简单的变动,先为球员增加薪水属性:
![](http://img.my.csdn.net/uploads/201301/22/1358822033_8468.png)
然后创建一名球员,信息如下:
![](http://img.my.csdn.net/uploads/201301/22/1358822051_2261.png)
这时候我们打算为球员调薪,比如上涨10%。为了结合NSMappingModel,这里简单地增加了一个新的属性newSalary,并且希望在数据迁移时更新该属性。为此,我们创建了一个NSMappingModel映射模型:
![](http://img.my.csdn.net/uploads/201301/22/1358822104_9477.png)
![](http://img.my.csdn.net/uploads/201301/22/1358822120_3729.png)
![](http://img.my.csdn.net/uploads/201301/22/1358822129_9552.png)
选择好源数据模型和目标数据模型,设置newSalary和salary的关系:
![](http://img.my.csdn.net/uploads/201301/22/1358822155_1229.png)
这表示目标属性newSalary的值为源属性salary的1.1倍。
这个时候,我们不希望Core Data自动为我们映射模型,所以修改一下迁移选项:
[cpp] view
plaincopy
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];
把NSInferMappingModelAutomaticallyOption设置为NO后,我们需要手工指定映射模型:
[cpp] view
plaincopy
NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"];
NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];
NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];
接着,进行实质性的数据迁移。简单起见,这里就没有做错误检查了:
[cpp] view
plaincopy
bf06
NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
if (![migrationManager migrateStoreFromURL:storeURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel toDestinationURL:tmpStoreURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {
NSLog(@"Error migrating %@, %@", error, [error userInfo]);
abort();
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *oldStoreName = @"cdNBA_old.sqlite";
NSURL *oldStoreURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:oldStoreName]];
[fileManager moveItemAtURL:storeURL toURL:oldStoreURL error:&error];
[fileManager moveItemAtURL:tmpStoreURL toURL:storeURL error:&error];
再跑一遍Demo,然后在终端里查看:
![](http://img.my.csdn.net/uploads/201301/22/1358822268_7261.png)
可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值:
![](http://img.my.csdn.net/uploads/201301/22/1358822296_1468.png)
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
Brief Talk About Core Data Series, Part 9 : Using Mapping Model
相关文章推荐
- Android WebView初体验
- android差异化更新(增量更新)
- Adapter模式实战-重构鸿洋的Android建行圆形菜单
- “煎蛋”Android版的高仿GitHub路径
- Android中Application类用法
- ExpandableListView的使用
- 微信支付的一些坑
- 一些iOS前辈的博客地址
- iOS alpha 和 hidden 的使用。
- appium第一次尝试
- iOS多语言工具genstrings for Localizable.strings
- Android统计图表MPAndroidChart
- Unity3D资源目录及资源读取
- appium环境安装
- coreData 深入理解3 (iOS5 以前线程安全与同步)
- 微信支付商户平台
- iOS生命周期及loadView、viewDidLoad及viewDidUnload的关系
- Android 数据库管理— — —升级数据库
- Android 把图片进行压缩
- CoreData 深入理解2 (iOS5 以后线程安全与同步)