java.lang.IllegalArgumentException:如果用于打开同一个文件,配置不能不同



我在领域面临这个问题。 我在活动中使用了领域配置。以下代码是活动中的领域配置

RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(1) // Must be bumped when the schema changes
.migration(new Migration()) // Migration to run
.build();
Realm.setDefaultConfiguration(config);
//      Realm.deleteRealm(config);
realm.getDefaultInstance();

我在广播接收器中使用了 Realm 配置来跟踪传入和传出的呼叫。接收器中使用以下代码

public void onReceive(Context context, Intent intent) {
mErrorString = new SparseIntArray();
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder()
.schemaVersion(1) // Must be bumped when the schema changes
.migration(new Migration()) // Migration to run
.build();
realm = Realm.getInstance(config);
//      realm = Realm.getDefaultInstance();
mediaRecorder = new MediaRecorder();
realmDbHelper = new RealmDbHelper();

在上面的代码中,我使用了 RealmDbHelper 类。 RealmDbHelper 类用于在单独的类中创建领域添加、查询、删除函数。异常发生在 RealmDbHelper 类中。

Realm 在 RealmDbHelper 类中初始化

public class RealmDbHelper  {
Realm realm = Realm.getDefaultInstance();
public RealmDbHelper(){
}

异常发生在"Realm realm = Realm.getDefaultInstance();

我该如何解决这个问题?

日志猫代码是

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.seyali.callLog, PID: 32039
java.lang.RuntimeException: Unable to start receiver com.seyali.callLog.receiver.CallReceiver: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
Cached configuration: 
realmDirectory: /data/user/0/com.seyali.callLog/files
realmFileName : default.realm
canonicalPath: /data/data/com.seyali.callLog/files/default.realm
key: [length: 0]
schemaVersion: 1
migration: com.seyali.callLog.model.Migration@25
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@2c1f16df
readOnly: false
New configuration: 
realmDirectory: /data/user/0/com.seyali.callLog/files
realmFileName : default.realm
canonicalPath: /data/data/com.seyali.callLog/files/default.realm
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@2c1f16df
readOnly: false
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap18(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1561)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
Cached configuration: 
realmDirectory: /data/user/0/com.seyali.callLog/files
realmFileName : default.realm
canonicalPath: /data/data/com.seyali.callLog/files/default.realm
key: [length: 0]
schemaVersion: 1
migration: com.seyali.callLog.model.Migration@25
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@2c1f16df
readOnly: false
New configuration: 
realmDirectory: /data/user/0/com.seyali.callLog/files
realmFileName : default.realm
canonicalPath: /data/data/com.seyali.callLog/files/default.realm
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: false
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@2c1f16df
readOnly: false
at io.realm.RealmCache.validateConfiguration(RealmCache.java:461)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:337)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284)
at io.realm.Realm.getDefaultInstance(Realm.java:279)
at com.seyali.callLog.util.RealmDbHelper.<init>(RealmDbHelper.java:20)
at com.seyali.callLog.receiver.CallReceiver.onReceive(CallReceiver.java:101)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3040)
  ... 8 more

这是我的领域迁移类

public class Migration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion==0){
oldVersion ++;
}
}
@Override
public int hashCode() {
return 37;
}
@Override
public boolean equals(Object o) {
return (o instanceof Migration);
}

}

我遇到了同样的问题,但在后台使用接收器进行 gcm 通知

这是我使用的解决方法。

我的接收类:

public class MyReceiver extends WakefulBroadcastReceiver {
private Context context;
private LocalBroadcastManager broadcaster;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
broadcaster = LocalBroadcastManager.getInstance(context);
Realm.init(context);
Realm realm = Realm.getDefaultInstance();
//Do something
}
}

领域访问类:

public class SplashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Realm.init(this);
Realm.removeDefaultConfiguration();
Realm.setDefaultConfiguration(new MyRealmConfiguration
.Builder()
.schemaVersion(0) // Must be bumped when the schema changes
.deleteRealmIfMigrationNeeded() // Migration to run instead of throwing an exception
.build());
Realm realm = Realm.getDefaultInstance();
}
}

Realm.removeDefaultConfiguration();

以前

Realm.setDefaultConfiguration(CONFIG);

这样,您可以使用相同的迁移类设置相同的配置,而不会触发预期;

此外,不需要单一实例 Realm 实例,因为领域实例是线程安全的 领域文档

最新更新