linyawen

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

 

  关于coredata 网上的相关资料比较少,大部分是基本用法。于是便找到苹果官方文档进行深入学习。

  分享一下心得,如果用了coredata 必须懂得 coredata Migration,否则app版本更新 core data model schema 变化很大可能导致持久化coredata 出错,程序崩溃。

  以下几种情况 可以简单的用  Lightweight Migration 迁移数据到新版本Model, 如下。

   NSDictionary *options = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,     [NSNumbernumberWithBool:YES], NSInferMappingModelAutomaticallyOptionnil];

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinatorallocinitWithManagedObjectModel:[selfmanagedObjectModel]];

    if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:storeURL options:options error:&error]) 

  //其他代码

--------------几种情况概括如下--------------------

    1,Simple addition of a new attribute.(为某个Entity 添加属性)

    2,Removal of an attribute.(为某个Entity 删除属性)

    3,A non-optional attribute becoming optional.

    4,An optional attribute becoming non-optional, and defining a default value.

    5,Renaming an entity or property.

 

  • Adding relationships and changing the type of relationship

    • You can add a new relationship or delete an existing relationship.

    • Renaming a relationship (by using a renaming identifier, just like an attribute)

    • Changing a relationship from a to-one to a to-many, or a non-ordered to-many to ordered (and visa-versa)

  • Changing the entity hierarchy

    • You can add, remove, rename entities

    • You can create a new parent or child entity and move properties up and down the entity hierarchy

    • You can move entities out of a hierarchy。

  

 

  <注意>You cannot, however, merge entity hierarchies; if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination。这种情况不能用轻量迁移。

官方文档:

 https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmLightweightMigration.html#//apple_ref/doc/uid/TP40004399-CH4-SW1

   下篇 《 core data model mapping 》

 

 

  

posted on 2013-06-05 20:15  linyawen  阅读(390)  评论(0编辑  收藏  举报