coredata Lightweight Migration 心得

來源:互聯網
上載者:User

 

 

  關於coredata 網上的相關資料比較少,大部分是基本用法。於是便找到蘋果官方文檔進行深入學習。

  分享一下心得,如果用了coredata 必須懂得 coredata Migration,否則app版本更新 core data model schema 變化很大可能導致持久化coredata 出錯,程式崩潰。

  以下幾種情況 可以簡單的用  Lightweight Migration 遷移資料到新版本Model, 如下。

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

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:[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 》

 

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.