`
dcj3sjt126com
  • 浏览: 1825308 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Core Data浅谈系列之九 : 使用Mapping Model

    博客分类:
  • IOS
阅读更多

通常,我们都会尽量使数据模型的变化尽量简单。但有些情况下,不得不进行大的改动,甚至是重新设计数据模型。在这种情况下,之前提过的简单数据迁移已经无法适应了,需要引入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自动为我们映射模型,所以修改一下迁移选项: 
  1. NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:  
  2. [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  
  3. [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];  
把NSInferMappingModelAutomaticallyOption设置为NO后,我们需要手工指定映射模型:
  1. NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"];  
  2. NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];  
  3. NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];  
接着,进行实质性的数据迁移。简单起见,这里就没有做错误检查了:
  1. NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];  
  2.   
  3. if (![migrationManager migrateStoreFromURL:storeURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel  toDestinationURL:tmpStoreURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {  
  4.     NSLog(@"Error migrating %@, %@", error, [error userInfo]);  
  5.     abort();  
  6. }  
  7.   
  8. NSFileManager *fileManager = [NSFileManager defaultManager];  
  9. NSString *oldStoreName = @"cdNBA_old.sqlite";  
  10. NSURL *oldStoreURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:oldStoreName]];  
  11. [fileManager moveItemAtURL:storeURL toURL:oldStoreURL error:&error];  
  12. [fileManager moveItemAtURL:tmpStoreURL toURL:storeURL error:&error];  
再跑一遍Demo,然后在终端里查看:



可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值: 
 
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
分享到:
评论

相关推荐

    Pro Entity Framework Core 2 for ASP.NET Core MVC

    Model, map, and access data effectively with Entity Framework Core 2, the latest evolution of Microsoft’s object-relational mapping framework that allows developers to access data using .NET objects,...

    Pro Core Data for iOS.pdf

    15 Adding the Core Data Framework...............................................................................................15 Creating the Data Model ...............................................

    Core Software Security: Security at the Source

    Mapping the Security Development Lifecycle to the Software Development Lifecycle Software Development Methodologies Waterfall Development Agile Development Chapter Summary References Security ...

    Mayavi 参考

    Scenes, data sources, and visualization modules: the pipeline model Loading data into Mayavi Installation Installing ready-made distributions Requirements for manual installs Doing it yourself: ...

    SAP BW经典教材,word版本,值得收藏

    The Data Model 5 Different Physical Tables 6 Integration and Transformation ProcessingGranular Data 8 Historical Data 9 Timestamping 10 Data Relationships 10 Generic Data versus Specific Data 11 Data ...

    Entity Framework 6 Recipes 第二版(英文高清pdf)

    EF是是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现在已开源,是.NET web开发首选ORM框架,其中的Link to Entity和Code First模式用起来非常过瘾,推荐学习使用! EF最新版本6.10,...

    Hibernate_Core_Reference_Guide.pdf

    Working with object-oriented software and a relational database can be cumbersome and time consuming in today's ...representation from an object model to a relational data model with a SQL-based schema.

    More iOS 6 Development Further Explorations of the iOS SDK

    Table of Contents * Here We Go Round Again * Core Data The What, Why & How * A Super Start: Adding, Displaying, and Deleting Data * The Devil in the Detail View * Preparing for Change: Migrations and...

    NHibernate 3.0 Cookbook

    Beginners will learn several techniques for each of the 4 core NHibernate tasks – mapping, configuration, session & transaction management, and querying – and which techniques fit best with various...

    ap6212a0_a33_sc3817r_服务器验证通过_bt已经通了_wifi需要修改配置_需要再次验证_20170626_1549.7z

    PRODUCT_MODEL := QUAD-CORE A33 y3 PRODUCT_MANUFACTURER := softwinner #include device/softwinner/polaris-common/prebuild/google/products/gms_base.mk 4、 R:\wyb\ap6212a0_a33_sc3817r\android\device\...

    EntityFramework入门教程实例

    Entity Data Model (EDM) 为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Schema 与 Storage Schema 三层,其上还有 Entity Client,Object Context 以及 LINQ 可以使用。

    Learning ExtJS(PACKT,4ed,2015)

    At the beginning, learn the core concepts of Sencha Ext JS, components, data models, and mapping. This book also teaches you about event-driven development, forms and grids, charts and themes, and ...

    cms后台管理

    return (List) super.getData(params, env); } } @Override protected boolean isPage() { return false; } } Content_list.html中的内容 [#list tag_list as a] <li><a href="${a.url}" target="_blank...

    游戏编程精粹8--随书光盘源代码(part1/2)

    1.6 Eye-View Pixel Anti-Aliasing for Irregular Shadow Mapping 74 1.7 Overlapped Execution on Programmable Graphics Hardware 90 1.8 Techniques for Effective Vertex and Fragment Shading on the SPUs ...

    游戏编程精粹8--随书光盘源代码(part2/2)

    本书为《Game Programming Gems》系列的第八卷。在这一版当中,我们将广泛地探讨重要的实时图形方面,如Larrabee, PlayStation 3, 以及DirectX 11计算着色器。此外在本书中, 处在第一线的业内顶级开发商如:...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    5.2.3. Passing model data to the view 5.3. Accepting request input 5.3.1. Taking query parameters 5.3.2. Taking input via path parameters 5.4. Processing forms 5.4.1. Writing a form-handling ...

    python3.6.5参考手册 chm

    Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already Present In Python 2.6 Library Changes PEP 3101: A New Approach To String ...

Global site tag (gtag.js) - Google Analytics