Core Data浅谈系列之四 : 数据模型的版本变迁
来源:互联网 发布:大司马淘宝店网址 编辑:程序博客网 时间:2024/05/03 10:16
继上一篇文章末尾提到的,一支队伍可以添加多名球员,不过一名球员只能属于一支队伍中,这分别对应着Core Data中一对多和一对一的属性关系:
如上两图,是在Team实体里面添加了一个players关系,指向Player实体,可以一支球队关联多名球员,并且最多只允许关联15名球员。
同样地,也为Player实体添加team关系,指向Team实体:
一名球员只能关联一支球队,并且让这个关系成双向的,即一个Player对象属于某支球队时,该球队的players属性就自动关联该Player对象。
做完以上对表关系的修改,再次运行程序。
Oops,运行不起来——
- 2013-01-16 16:56:18.667 cdNBA[18591:c07] Error : The operation couldn’t be completed. (Cocoa error 134100.)
- 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添加存储文件时设置一个选项:
- - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
- {
- if (nil != _persistentStoreCoordinator) {
- return_persistentStoreCoordinator;
- }
- NSString *storeType = NSSQLiteStoreType;
- NSString *storeName = @"cdNBA.sqlite";
- NSError *error = NULL;
- NSURL *storeURL = [NSURLfileURLWithPath:[[selfapplicationDocumentsDirectory] stringByAppendingPathComponent:storeName]];
- NSDictionary *options = [NSDictionarydictionaryWithObjectsAndKeys:
- [NSNumbernumberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
- [NSNumbernumberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
- _persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:self.managedObjectModel];
- if (![_persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nilURL:storeURL options:options error:&error]) {
- NSLog(@"Error : %@\n", [error localizedDescription]);
- NSAssert1(YES, @"Failed to create store %@ with NSSQLiteStoreType", [storeURL path]);
- }
- return _persistentStoreCoordinator;
- }
做完上面的工作,再跑一遍Demo。
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Core Data浅谈系列之十 : 关于数据模型中实体的属性
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列之二 : 简单的CURD
- Core Data浅谈系列汇总
- Core Data浅谈系列之三 : 了解NSManagedObject和NSPredicate
- Core Data浅谈系列之五 : 在UITableView中展示
- Core Data浅谈系列之六 : 验证用户输入
- Core Data浅谈系列之七 : 使用NSFetchedResultsController
- Core Data浅谈系列之八 : 关于并发
- App Stories
- 代理服务器和反向代理
- 结构体定义 typedef struct 用法详解和用法小结
- 基于WPF的简单画图
- MySQL,记录一些tips以备用
- Core Data浅谈系列之四 : 数据模型的版本变迁
- Flex编译器参数解析
- sigprocmask 用法
- 《Pragmatic unit testing:in java with Junit》阅读
- Core Data浅谈系列之五 : 在UITableView中展示
- 对 mysql Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (gb2312_chinese_ci,I的解决
- tapstream
- treeview控件的Expand事件下加载数据的控制
- Core Data浅谈系列之六 : 验证用户输入