根据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