如何获取领域路径以进行迁移?
How do I get the realm path in order to make a migration?
如果我尝试进行迁移,我将如何获得 realmPath?我目前有这样的东西:
try {
Realm realm = Realm.getInstance(this);
} catch (RealmMigrationNeededException e) {
Realm.migrateRealmAtPath(???, new Migration());
}
此外,我有以下版本:
- 版本 1 无领域
- 版本 2 领域
- 具有迁移的版本 3 领域
我如何确保从版本 1 升级到版本 3 的用户以及直接安装版本 3 的用户将更新到最新的架构版本号(因为他们不需要 运行移民)?
那些从 v2 到 v3 的人会因为迁移 运行ning 而获得版本提升,但不确定如何设置 Realm 架构版本。
这里是 Realm 的基督徒。当前的迁移 API 仍然非常粗糙,所以唯一的方法是进行只增加版本号的迁移。所以像下面这样的东西可能是目前最好的解决方法。
// Pseudo code
public static class RealmHelper {
private static SharedPreferences prefs;
public static Realm getInstance(Context context) {
String realmPath = new File(context.getFilesDir(), "default.realm").getAbsolutePath();
if (prefs.getBoolean("firstRun", true)) {
Realm.migrateRealmAtPath(realmPath, new RealmMigration() {
@Override
public long migrate(Realm realm, long oldVersion) {
return 42; // new version of Realm
}
});
prefs.edit().putBoolean("firstRun", false).commit();
}
try {
return Realm.getInstance(context);
} catch (RealmMigrationNeededException e) {
Realm.migrateRealmAtPath(realmPath, new CustomMigration());
return Realm.getInstance(context);
}
}
}
然而,我们希望很快能使它变得更好:https://github.com/realm/realm-java/pull/880
如果我尝试进行迁移,我将如何获得 realmPath?我目前有这样的东西:
try {
Realm realm = Realm.getInstance(this);
} catch (RealmMigrationNeededException e) {
Realm.migrateRealmAtPath(???, new Migration());
}
此外,我有以下版本:
- 版本 1 无领域
- 版本 2 领域
- 具有迁移的版本 3 领域
我如何确保从版本 1 升级到版本 3 的用户以及直接安装版本 3 的用户将更新到最新的架构版本号(因为他们不需要 运行移民)?
那些从 v2 到 v3 的人会因为迁移 运行ning 而获得版本提升,但不确定如何设置 Realm 架构版本。
这里是 Realm 的基督徒。当前的迁移 API 仍然非常粗糙,所以唯一的方法是进行只增加版本号的迁移。所以像下面这样的东西可能是目前最好的解决方法。
// Pseudo code
public static class RealmHelper {
private static SharedPreferences prefs;
public static Realm getInstance(Context context) {
String realmPath = new File(context.getFilesDir(), "default.realm").getAbsolutePath();
if (prefs.getBoolean("firstRun", true)) {
Realm.migrateRealmAtPath(realmPath, new RealmMigration() {
@Override
public long migrate(Realm realm, long oldVersion) {
return 42; // new version of Realm
}
});
prefs.edit().putBoolean("firstRun", false).commit();
}
try {
return Realm.getInstance(context);
} catch (RealmMigrationNeededException e) {
Realm.migrateRealmAtPath(realmPath, new CustomMigration());
return Realm.getInstance(context);
}
}
}
然而,我们希望很快能使它变得更好:https://github.com/realm/realm-java/pull/880