Docs 菜单

Docs 主页开发应用程序Atlas Device SDKs

接口 RealmMigration

在此页面上

  • io.realm
  • 方法摘要
  • 方法详细信息
  • 迁移

RealmMigration 类用于执行从一种 Realm 模式到另一种 Realm 模式的迁移。 Realm 的模式由项目中扩展io.realm.RealmObject或实现io.realm.RealmModel的所有类定义,因此对这些类的任何更改都需要进行迁移。

为了支持从任何以前的 schemaVersion 到最新 schemaVersion 的迁移,建议在编写迁移时使用以下模式:

public class CustomMigration implements RealmMigration {
@Override
public long migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
// Migrate from v0 to v1
oldVersion++;
}
if (oldVersion == 1) {
// Migrate from v1 to v2
oldVersion++;
}
if (oldVersion < newVersion) {
throw new IllegalStateException(String.format(Locale.US, "Migration missing from v%d to v%d", oldVersion, newVersion));
}
}
}

在开发过程中,当 RealmObject 类可能会频繁更改时,可以使用io.realm.Realm.deleteRealm(RealmConfiguration) 。 这将删除数据库文件并消除任何迁移的需要。

提示

另请参阅:

修饰符和类型
方法和说明
公共无效
long oldVersion,
long newVersion
)

如果需要迁移,则会调用此方法。

公共无效迁移
long oldVersion,
long newVersion
)

如果需要迁移,则会调用此方法。 整个方法包装在写事务(write transaction)中,因此可以创建、更新或删除任何现有对象,而无需将其包装在自己的事务中。

参数

  • realm - 要执行迁移的 Realm 模式。

  • oldVersion - 迁移开始时 Realm 的模式版本。

  • newVersion - 执行迁移后 Realm 的模式版本。

← 类 RealmMap