2014-03-27 169 views
1

我在iOS上的核心數據(SQLite)數據庫的新添加字段上遇到無法識別的選擇器。我使用Xcode的編輯器菜單添加了一個新的模型版本,然後驗證新版本是當前版本。我也確信修改過的表格的.h和.m文件已經更新,儘管我手動完成了這些工作(如何爲您生成這些文件?)。沒有什麼不尋常的,只是一個String類型的字段。輕量級核心數據遷移後出現無法識別的選擇器

問題似乎是輕量級遷移永遠不會發生在嘗試引用數據庫對象的時間代碼運行時。特林訪問newFieldName給出:

-[MyEntity newFieldName]: unrecognized selector sent to instance 0x852b510 
2014-03-27 13:26:21.734 ASSIST for iPad[41682:c07] *** Terminating app due to 
uncaught exception 'NSInvalidArgumentException', reason: '-[MyEntity newFieldName]: 
unrecognized selector sent to instance 0x852b510' 

的代碼生成上述錯誤的行是唯一的線爲下面循環:

DataStoreCoreData *dStore = [[DataStoreCoreData alloc] initWithDataItemDescription:did]; 

for (MyEntity *myEnt in [dStore objects]) 
    NSString *name = [myEnt newFieldName];  

如所提到的,當我檢查SQLite的分貝它具有沒有新的領域,這是錯誤的意義。因此,我還通過執行應該執行遷移的代碼的執行,它似乎工作正常。成功。它看起來像下面這樣:

NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"ASSIST.sqlite"]]; 

// handle db upgrade 
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

NSError *error = nil; 
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; 

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) 
    NSLog(@"\r\n Fail. [error localizedDescription]: %@", [error localizedDescription]); 
else 
    NSLog(@"\r\n Success"); 

這是第6個數據庫版本升級。所有這些都是輕量級遷移,沒有不尋常的問題。上面的代碼不應該強制SQLite數據庫反映新的模式?我該如何做到這一點,或者這裏還有其他問題?

回答

0

我終於發現了答案。我對問題所提供的代碼只是在對數據庫進行微小更改(輕量級遷移)時需要修改的代碼的一部分。創建對象模型時,您還需要指定新版本。請注意下面的代碼中的「MyNewVersion」。您應該更新此參數以反映您創建的新版本,然後選擇爲當前的型號版本:

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyNewVersion" ofType:@"mom" inDirectory:@"ASSIST.momd"]; 
NSURL *momURL = [NSURL fileURLWithPath:path]; 
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL]; 
相關問題