gpt4 book ai didi

java - recreate() 方法后迁移崩溃

转载 作者:行者123 更新时间:2023-12-01 10:07:19 26 4
gpt4 key购买 nike

迁移工作正常,但我的应用程序有时必须重新启动(recreate())才能执行界面操作。此时应用程序因迁移而崩溃:

java.lang.RuntimeException: Unable to start activity ComponentInfo{APP/APP.MainActivity}: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
Cached configuration:
realmFolder: /data/user/0/APP/files
realmFileName : default.realm
canonicalPath: /data/data/APP/files/default.realm
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@19c237a3

迁移 fragment :

mRealmConfig = new RealmConfiguration.Builder(this)
.schemaVersion(0)
.migration(new Migration())
.build();
realm = Realm.getInstance(mRealmConfig);
realm.close();

迁移类:

public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();

// Migrate from version 0 to version 1
if (oldVersion == 0) {
RealmObjectSchema notificationSchema = schema.get("classTarget");

notificationSchema
.addField("something", Boolean.class, FieldAttribute.REQUIRED)
.transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("something", true);
}
});
oldVersion++;
}
}
}

最佳答案

听起来您正在多次创建 RealmConfiguration 实例。尝试将其保留在静态字段中或使用 Realm.setDefaultInstance(config)/Realm.getDefaultInstance() 代替。

您看到异常的原因是您的 RealmMigration 类没有重写 equals()/hashCode()。这意味着我们的内部检查认为您有两种不同的配置,因为 new Migration().equals(new Migration()) == false

关于java - recreate() 方法后迁移崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com