2015-10-27 41 views
18

我有一個對象NotSureItem,其中有三個屬性'標題',其名稱已從'text'和textDescription中重新命名,後面我添加了一個dateTime屬性。現在當我要運行我的應用程序時,它會崩潰,當我想添加一些東西到這些屬性。它顯示了以下聲明。RLMException,對象類型需要遷移

'由於以下錯誤,對象類型'NotSureItem'需要遷移: - 最新對象模型缺少屬性「文本」。 - 屬性'標題'已被添加到最新的對象模型。 - Property'textDescription'已被添加到最新的對象模型中。'

這裏的我的代碼:

import Foundation 
import Realm 

class NotSureItem: RLMObject { 
    dynamic var title = "" // renamed from 'text' 
    dynamic var textDescription = "" // added afterwards 
    dynamic var dateTime = NSDate() 
} 

回答

74

只要刪除您的應用程序並重新運行。

每當您更改Realm對象的屬性時,您現有的數據庫將與新的對象不兼容。

只要你還處於開發階段,你可以簡單地從模擬器/設備上刪除應用程序,然後重新啓動它。

當您的應用程序發佈後,如果您更改了對象的屬性,則必須實施遷移到新的數據庫版本。

+0

爲什麼downvote? – joern

+1

它適合我@joern –

+2

這是正確的答案。只是有人低估了它。你能否接受答案,如果它爲你工作,所以這被標記爲一個正確的答案? – joern

5

下面的代碼爲我工作

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration]; 
config.schemaVersion = 2; 
config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) { 
    // The enumerateObjects:block: method iterates 
    // over every 'Person' object stored in the Realm file 
    [migration enumerateObjects:Person.className 
        block:^(RLMObject *oldObject, RLMObject *newObject) { 
    // Add the 'fullName' property only to Realms with a schema version of 0 
    if (oldSchemaVersion < 1) { 
     newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", 
          oldObject[@"firstName"], 
          oldObject[@"lastName"]]; 
    } 

    // Add the 'email' property to Realms with a schema version of 0 or 1 
    if (oldSchemaVersion < 2) { 
    newObject[@"email"] = @""; 
    } 
    }]; 
}; 
[RLMRealmConfiguration setDefaultConfiguration:config]; 

// now that we have updated the schema version and provided a migration block, 
// opening an outdated Realm will automatically perform the migration and 
// opening the Realm will succeed 
[RLMRealm defaultRealm]; 

return YES; 
} 

更多信息:https://realm.io/docs/objc/latest/#getting-started

3

你修改數據庫不再與保存的數據庫這就是爲什麼需要遷移兼容。您的選擇是刪除舊的數據庫文件並重新啓動(如果您處於初始開發階段,則工作良好),或者如果您在現場,請執行遷移。

您可以通過定義架構版本並在您的Realm配置中提供數據庫遷移「腳本」來實現此目的。整個過程記錄在這裏(連同代碼樣本):here

2

您可以在啓動消除數據庫是這樣的:

[[NSFileManager defaultManager] removeItemAtURL:[RLMRealmConfiguration defaultConfiguration].fileURL error:nil]; 
10

刪除應用程序,並重新安裝是不是一個好的做法。我們應該在第一次遇到移民需求時,在開發過程中納入一些遷移步驟。由SilentDirge提供的鏈接很好:realm migration document,它爲處理不同情況提供了很好的示例。

最少遷移任務,從上面的鏈接下面的代碼片段可以自動完成遷移,並與AppDelegate中的disFinishLaunchWithOptions方法一起使用:

let config = Realm.Configuration(
 
    // Set the new schema version. This must be greater than the previously used 
 
    // version (if you've never set a schema version before, the version is 0). 
 
    schemaVersion: 1, 
 

 
    // Set the block which will be called automatically when opening a Realm with 
 
    // a schema version lower than the one set above 
 
    migrationBlock: { migration, oldSchemaVersion in 
 
    // We haven’t migrated anything yet, so oldSchemaVersion == 0 
 
    if (oldSchemaVersion < 1) { 
 
     // Nothing to do! 
 
     // Realm will automatically detect new properties and removed properties 
 
     // And will update the schema on disk automatically 
 
    } 
 
    }) 
 

 
// Tell Realm to use this new configuration object for the default Realm 
 
Realm.Configuration.defaultConfiguration = config 
 

 
// Now that we've told Realm how to handle the schema change, opening the file 
 
// will automatically perform the migration 
 
let _ = try! Realm()

相關問題