Core Data浅谈系列之九 : 使用Mapping Model
2013-01-22 10:33
92 查看
通常,我们都会尽量使数据模型的变化尽量简单。但有些情况下,不得不进行大的改动,甚至是重新设计数据模型。在这种情况下,之前提过的简单数据迁移已经无法适应了,需要引入Mapping Model这个中间层。
这时,又想起之前提过的一句话:
There is no problem in computer
science that can’t be solved by adding another level of indirection.
这里做一个简单的变动,先为球员增加薪水属性:
然后创建一名球员,信息如下:
这时候我们打算为球员调薪,比如上涨10%。为了结合NSMappingModel,这里简单地增加了一个新的属性newSalary,并且希望在数据迁移时更新该属性。为此,我们创建了一个NSMappingModel映射模型:
选择好源数据模型和目标数据模型,设置newSalary和salary的关系:
这表示目标属性newSalary的值为源属性salary的1.1倍。
这个时候,我们不希望Core Data自动为我们映射模型,所以修改一下迁移选项:
可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值:
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
Brief Talk About Core Data Series, Part 9 : Using Mapping Model
Jason Lee @ Hangzhou
Blog : http://blog.csdn.net/jasonblog
Weibo : http://weibo.com/jasonmblog
这时,又想起之前提过的一句话:
There is no problem in computer
science that can’t be solved by adding another level of indirection.
这里做一个简单的变动,先为球员增加薪水属性:
然后创建一名球员,信息如下:
这时候我们打算为球员调薪,比如上涨10%。为了结合NSMappingModel,这里简单地增加了一个新的属性newSalary,并且希望在数据迁移时更新该属性。为此,我们创建了一个NSMappingModel映射模型:
选择好源数据模型和目标数据模型,设置newSalary和salary的关系:
这表示目标属性newSalary的值为源属性salary的1.1倍。
这个时候,我们不希望Core Data自动为我们映射模型,所以修改一下迁移选项:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];把NSInferMappingModelAutomaticallyOption设置为NO后,我们需要手工指定映射模型:
NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"]; NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath]; NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];接着,进行实质性的数据迁移。简单起见,这里就没有做错误检查了:
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,然后在终端里查看:
可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值:
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
Brief Talk About Core Data Series, Part 9 : Using Mapping Model
Jason Lee @ Hangzhou
Blog : http://blog.csdn.net/jasonblog
Weibo : http://weibo.com/jasonmblog
相关文章推荐
- Core Data浅谈系列之九 : 使用Mapping Model
- Core Data浅谈系列之九 : 使用Mapping Model
- Core Data浅谈系列之九 : 使用Mapping Model
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- FMS3系列(六):使用远程共享对象(SharedObject)实现多人时时在线聊天(Flex | Flash)
- Java 集合系列03之 ArrayList详细介绍(源码解析)和使用示例
- JetBrain的系列IDE中Tasks插件的使用经验和几点建议
- [转]Ultra Fractal教程系列48——动画功能的使用06——时间轴工具的使用
- Cordova各个插件使用介绍系列(四)—canvas2ImagePlugin保存二维码到手机本地
- C#强化系列文章一:ViewState使用兼谈序列化 [转]
- 安卓系列三(使用pull 方式解析xml文件)
- 1 开发一个注重性能的JDBC应用程序不是一件容易的事. 当你的代码运行很慢的时候JDBC驱动程序并不会抛出异常告诉你。 本系列的性能提示将为改善JDBC应用程序的性能介绍一些基本的指导原则,这其中的原则已经被许多现有的JDBC应用程序编译运行并验证过。 这些指导原则包括: 正确的使用数据库MetaData方法 只获取需要的数据 选用最佳性能的功能 管理连
- JQuery中html、append、appendTo、after、insertAfter、before、insertBefore、empty、remove系列方法的使用
- C++语言笔记系列之七——变量的作用域和使用方法
- solr系列--solrj使用