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

Core Data浅谈系列之四 : 数据模型的版本变迁

    博客分类:
  • IOS
阅读更多
上一篇文章末尾提到的,一支队伍可以添加多名球员,不过一名球员只能属于一支队伍中,这分别对应着Core Data中一对多和一对一的属性关系:
 

 

 
如上两图,是在Team实体里面添加了一个players关系,指向Player实体,可以一支球队关联多名球员,并且最多只允许关联15名球员。
同样地,也为Player实体添加team关系,指向Team实体:

 
一名球员只能关联一支球队,并且让这个关系成双向的,即一个Player对象属于某支球队时,该球队的players属性就自动关联该Player对象。
 
做完以上对表关系的修改,再次运行程序。
Oops,运行不起来——
[plain] view plaincopy
  1. 2013-01-16 16:56:18.667 cdNBA[18591:c07] Error : The operation couldn’t be completed. (Cocoa error 134100.)  
  2. 2013-01-16 16:56:18.670 cdNBA[18591:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'  
这是由于我们刚才做了一番修改,persistentStoreCoordinator无法适应新的数据模型。
当开启一个新版本时,如果数据模型发生变动,我们需要创建一个新版本使用的数据模型:

 
并将其设置为当前版本使用的数据模型:

 

 
So,我们刚才发生的变动都应该应用在cdNBA 2.xcdatamodel上。
除此之外,还需要在persistentStoreCoordinator添加存储文件时设置一个选项:
  1. - (NSPersistentStoreCoordinator *)persistentStoreCoordinator  
  2. {  
  3.     if (nil != _persistentStoreCoordinator) {  
  4.         return_persistentStoreCoordinator;  
  5.     }  
  6.       
  7.     NSString *storeType = NSSQLiteStoreType;  
  8.     NSString *storeName = @"cdNBA.sqlite";  
  9.       
  10.     NSError *error = NULL;  
  11.     NSURL *storeURL = [NSURLfileURLWithPath:[[selfapplicationDocumentsDirectory] stringByAppendingPathComponent:storeName]];  
  12.       
  13.     NSDictionary *options = [NSDictionarydictionaryWithObjectsAndKeys:  
  14.      [NSNumbernumberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,  
  15.      [NSNumbernumberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];  
  16.       
  17.     _persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:self.managedObjectModel];  
  18.     if (![_persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nilURL:storeURL options:options error:&error]) {  
  19.         NSLog(@"Error : %@\n", [error localizedDescription]);  
  20.         NSAssert1(YES, @"Failed to create store %@ with NSSQLiteStoreType", [storeURL path]);  
  21.     }  
  22.       
  23.     return _persistentStoreCoordinator;  
  24. }  
做完上面的工作,再跑一遍Demo。

 

分享到:
评论

相关推荐

    rack-core-data, 从核心数据模型自动生成 restapi.zip

    rack-core-data, 从核心数据模型自动生成 restapi Rack::CoreData自动生成核心数据模型的restapi这个项目被反对of在 Rack::Scaffold & core_data中分离功能。 按照 Helios 了解有关这里项目及其相关项目的更新。核心...

    Core.Data.in.Swift.Data.Storage.and.Management.for.iOS.and.OSX

    Core Data is intricate, powerful, and necessary. Discover the powerful capabilities integrated into Core Data, and how to use Core Data in your iOS and OS X projects. All examples are current for OS X...

    Core Data: Updated for Swift 3

    This book strives to give you clear guidelines for how to get the most out of Core Data while avoiding the pitfalls of this flexible and powerful framework. We start with a simple example app and ...

    Core Data 数据自动封装

    能够十分方便地为 Core Data 数据自动封装CRUD(创建,读取,更新和删除)操作。并且指定排序的域,可以自动为Core Data 数据加上按照这个域的排序操作。 Demo的演示了CRUD操作以及索引排序操作,比如自动为小编...

    Data Analytics with Hadoop: An Introduction for Data Scientists

    "Data Analytics with Hadoop: An Introduction for Data Scientists" ISBN: 1491913703 | 2016 | PDF | 288 pages | 7 MB Ready to use statistical and machine-learning techniques across large data sets? ...

    Core Data by Tutorials 3rd

    What is Core Data? You'll hear a variety of answers to this question: It’s a database! It's SQLite! It's not a database! And so forth. Here's the technical answer: Core Data is an object graph ...

    Core Data by Tutorials v4.0 (Swift 4)

    Core Data by Tutorials v4.0 Core Data by Tutorials v4.0

    列表显示 core data 数据

    使用NSFetchedResultsController,uitableView 显示Core Data数据,不需要reload data,类表数据自动更新

    Core Data例子3

    Core Data允许用户使用代表实体和实体间关系的高层对象来操作数据。它也可以管理串行化的数据,提供对象生存期管理与object graph管理,包括存储。Core Data直接与SQLite交互,避免开发者使用原本的SQL语句

    Core Data例子4

    Core Data允许用户使用代表实体和实体间关系的高层对象来操作数据。它也可以管理串行化的数据,提供对象生存期管理与object graph管理,包括存储。Core Data直接与SQLite交互,避免开发者使用原本的SQL语句

    Core Data例子2

    Core Data允许用户使用代表实体和实体间关系的高层对象来操作数据。它也可以管理串行化的数据,提供对象生存期管理与object graph管理,包括存储。Core Data直接与SQLite交互,避免开发者使用原本的SQL语句

    Core Data 数据操作

    代码展示了Core Data添加、修改、删除、查询数据,并且将数据保存到本地

    Core Data数据验证

    这一段代码具体实现了如何验证输入数据的合法性。

    Core Data by Tutorials v4.0 Source Code (Swift4)

    Core Data by Tutorials v4.0 Core Data by Tutorials v4.0

    Core Data例子5

    Core Data允许用户使用代表实体和实体间关系的高层对象来操作数据。它也可以管理串行化的数据,提供对象生存期管理与object graph管理,包括存储。Core Data直接与SQLite交互,避免开发者使用原本的SQL语句

    Core Data objc

    Core Data objc Core Data objc Core Data objc Core Data objc Core Data objc

    core data数据操作实例代码

    core data数据操作实例代码 ios 实现对数据的插入,查询,删除等操作

    Pro Core Data for IOS

    Fully updated for Xcode 4.2, Pro Core Data for iOS explains how to use the Core Data framework for iOS SDK 5 using Xcode 4.2.

    Data Driven: Harnessing Data and AI to Reinvent Customer Engagement

    The indispensable guide to data-powered marketing from the team behind the data management platform that helps fuel Salesforce―the #1 customer relationship management (CRM) company in the world ...

    Core Data多线程大量数据同步

    Core Data多线程大量数据同步,ios开发

Global site tag (gtag.js) - Google Analytics