2012-04-29 27 views
2

我已經打了這個問題,coredata和它的駕駛我堅果,因爲它應該是直向前更改CoreData型號:復古兼容性

我目前正在對這個應用程序的第一個版本,操作系統顯然我一直在這裏和那裏搜索核心數據模型,但是每次更改核心數據模型時,都需要卸載應用程序並重新安裝新版本。

這是可以通過,雖然它只是我,但一旦發佈我需要能夠更改應用程序,而無需我的用戶重新安裝。

我錯過了什麼,

有一些代碼,我要寫信告訴核心數據如何將已有的持續性數據修改爲新的?

感謝您的幫助

傑森

回答

6

核心數據模型 - 遷移 - 添加新的屬性/字段當前數據模型 - 沒有仿真器或應用程序的RESET需要

步驟:

1)創建從編輯器模型版本 - 給它任何有意義的名稱,如ModelVersion2

2)進入該模型版本,並進行更改到你的模型。

3)現在進入YourProjectModel.xcdatamodeld並將當前版本設置爲新創建的版本。

4)添加下面的代碼放置在其中創建持久的協調 -

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

並設置選項值作爲方法的選擇 -

[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error] 

對我來說,它看起來是這樣的:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 

if (__persistentStoreCoordinator != nil) { 
    return__persistentStoreCoordinator; 
} 

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 

[NSNumber numberWithBool:YES],  NSMigratePersistentStoresAutomaticallyOption, 

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 


NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LGDataModel.sqlite"]; 

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (! [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) 
{ 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
    } 

    return__persistentStoreCoordinator; 
} 

鏈接:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmInitiating.html#//apple_ref/doc/uid/TP40004399-CH7-SW1