Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file.?

Tags:

android

realm

i am facing this issue in realm . I used the realm configuration in activity. The below code is realm configuration in activity

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

I used the Realm configuration in broadcast receiver for tracking the incoming and outgoing calls. The below code is used in receiver

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

In the above code i used the RealmDbHelper class. RealmDbHelper class is used for realm adding,querying, deletion function are created in separate class. The exception is occurring in the RealmDbHelper class.

The Realm initializing in RealmDbHelper class

public class RealmDbHelper  {

Realm realm = Realm.getDefaultInstance();

public RealmDbHelper(){

}

The exception is occurring in the line `Realm realm = Realm.getDefaultInstance();

How can i solve this issue ?

The log cat code is

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

This is my realm migration class

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

}

like image 796
Prabha Karan Avatar asked Oct 23 '25 23:10

Prabha Karan


1 Answers

I had this same problem but using a receiver for gcm notifications in background

This is a work-around I used.

My receiver class:

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 class:

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

    }
}

Use

Realm.removeDefaultConfiguration();

Before

Realm.setDefaultConfiguration(CONFIG);

This way you can set the same configuration with the same migration class without triggering the expection;

Also, there's no need for a singleton Realm instance since realm instances are thread-safe Realm Documentation

like image 81
Ítalo Lessa Avatar answered Oct 26 '25 13:10

Ítalo Lessa