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

时间:2017-07-05 10:19:39

标签: android realm

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

       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();

我该如何解决这个问题?

日志cat代码是

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);
}

}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但在背景中使用接收器进行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
   }
}

Realm Access类:

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实例,因为领域实例是线程安全的 Realm Documentation