根據the Realm documentation,有一些如何在Realm中進行遷移的示例。
從示例代碼,還有就是:
// define a migration block
// you can define this inline, but we will reuse this to migrate realm files from multiple versions
// to the most current version of our data model
RLMMigrationBlock migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
if (oldSchemaVersion < 1) {
[migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
if (oldSchemaVersion < 1) {
// combine name fields into a single field
newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@", oldObject[@"firstName"], oldObject[@"lastName"]];
}
}];
}
if (oldSchemaVersion < 2) {
[migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
// give JP a dog
if ([newObject[@"fullName"] isEqualToString:@"JP McDonald"]) {
Pet *jpsDog = [[Pet alloc] initWithValue:@[@"Jimbo", @(AnimalTypeDog)]];
[newObject[@"pets"] addObject:jpsDog];
}
}];
}
if (oldSchemaVersion < 3) {
[migration enumerateObjects:Pet.className block:^(RLMObject *oldObject, RLMObject *newObject) {
// convert type string to type enum if we have outdated Pet object
if (oldObject && oldObject.objectSchema[@"type"].type == RLMPropertyTypeString) {
newObject[@"type"] = @([Pet animalTypeForString:oldObject[@"type"]]);
}
}];
}
NSLog(@"Migration complete.");
它看起來像你聲明中,你列舉的對象和手動更新架構的模塊。
這可能取決於您的數據存儲現在的樣子。你有你的數據在覈心數據存儲或NSUserDefaults?除此之外,請參閱Realm入門指南。這個很不錯。 – Moshe
沒有使用核心數據。由於該應用程序仍處於開發階段,同時部署了專門構建並更改了領域數據庫結構。應用程序會崩潰,除非我們進行全新安裝。 – silentBeep
什麼樣的崩潰?你可以在原始問題中分享一個日誌嗎? – Moshe