java - 迁移时如何删除Realm?

标签 java android migration realm

这就是我想要的:

public class CustomRealmMigration implements RealmMigration {
    // Current version
    private static final long SCHEMA_VERSION = 4;

    private final Context mContext;

    public CustomRealmMigration(Context context) {
        mContext = context;
    }

    @Override
    public long execute(Realm realm, long version) {
        if (SCHEMA_VERSION < version) {
            // Rollback, not allow
            deleteRealm();
            return SCHEMA_VERSION;
        }
        return version;
    }

    private void deleteRealm() {
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(mContext)
                .name(Realm.DEFAULT_REALM_NAME)
                .build();
        Realm.deleteRealm(realmConfiguration);
    }
}

有错误:

Caused by: java.lang.IllegalStateException: It's not allowed to delete the file associated with an open Realm. Remember to close() all the instances of the Realm before deleting its file.

迁移时如何删除Realm?或者如何在其他地方获取旧版本的 Realm?

最佳答案

如果您不想在架构更改时迁移数据,而只是重置/清除数据库,那么您可以执行以下操作:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(mContext)
                .name(Realm.DEFAULT_REALM_NAME)
                .deleteRealmIfMigrationRequired()
                .build();

这也意味着您不必提供任何迁移代码。

关于java - 迁移时如何删除Realm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754158/

相关文章:

java - 全屏边框问题

java - 反编译的 Java 类产生不同的输出

android - init.rc 中的 mkdir

android - 在 android 的 ListView 中每 5 秒显示一次纬度和经度列表

android - 在单击按钮时将对象作为参数传递

migration - 在 IE11 中键入 "magic"自动启动 Edge (Chromium) 时,是什么导致 "cnn.com"?

java - 如果 JDialog 可见,JLabel 会更改文本

java - 使用 Selenium-Java 单击 'find hotels' 时出现问题

python - South 不会为现有应用程序生成或应用迁移,更改要迁移

typescript - TypeORM:无法读取未定义的属性 'id'