migration quesstion
i just update the schema version, and add two tables. when i update the apk and run , it always thrown this exception,i have no idea, please help me.
12-09 17:07:28.035 23920-23920/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.superlity.hiqianbei, PID: 23920 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.superlity.hiqianbei/com.superlity.hiqianbei.ui.activity.MainActivity_}: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. Cached configuration: realmFolder: /data/data/com.superlity.hiqianbei/files realmFileName : realmData canonicalPath: /data/data/com.superlity.hiqianbei/files/realmData key: [length: 0] schemaVersion: 1 migration: com.superlity.hiqianbei.d.g@42e43ff0 deleteRealmIfMigrationNeeded: false durability: FULL schemaMediator: io.realm.DefaultRealmModuleMediator@9df57fb8 12-09 17:07:28.043 2068-2490/? I/KLO_Stability: [am_crash][1449652048]:[23920,0,com.superlity.hiqianbei,14204484,java.lang.IllegalArgumentException,Configurations cannot be different if used to open the same file. Cached configuration: realmFolder: /data/data/com.superlity.hiqianbei/files realmFileName : realmData canonicalPath: /data/data/com.superlity.hiqianbei/files/realmData key: [length: 0] schemaVersion: 1 migration: com.superlity.hiqianbei.d.g@42e43ff0 deleteRealmIfMigrationNeeded: false durability: FULL schemaMediator: io.realm.DefaultRealmModuleMediator@9df57fb8
Can you post how you are creating the configuration as well as getting the Realm instance?
We just updated to 0.86.0 from 0.82.1 and now this is happening to us, as well. The code to create the config and set the default instance has not changed.
RealmConfiguration config = new RealmConfiguration.Builder(getContext())
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file.
Cached configuration:
realmFolder: /data/data/edu.osu.osumobile.dev/files
realmFileName : default.realm
canonicalPath: /data/data/edu.osu.osumobile.dev/files/default.realm
key: [length: 0]
schemaVersion: 0
migration: null
deleteRealmIfMigrationNeeded: true
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@e64f04b0
This looks like it could be caused if a Realm db is being opened multiple times concurrently.
A workaround seems to be if you create a singleton/static RealmConfiguration and use it for all your instances lookups.
ie.
private static mRealmConfig;
if (mRealmConfig == null) {
mRealmConfig = new RealmConfiguration.Builder(context)
.migration(new Migration())
.build();
}
Realm realm = Realm.getInstance(mRealmConfig);
Make sure you're closing your realm instances when you're done with them though.
I have a feeling our hashing/equals function comparing two Realms doesn't like it is supposed to. In the meantime a workaround is saving the configuration in a static field and reuse that (it will also be more performant).
This looks like it's specifically related to this line of RealmConfiguration (https://github.com/realm/realm-java/blob/master/realm/realm-library/src/main/java/io/realm/RealmConfiguration.java#L160).
if (migration != null ? !migration.equals(that.migration) : that.migration != null) return false;
Alternatively you can work around this issue by creating a singleton out of your migration class.
@corcoran Thanks for the detailed report!
@corcoran Thanks for the info.
Ideally you should only create one instance of each RealmConfiguration anyway, so I am tempted to categorize this as "working as intended". That said I can see why the error message can be very misleading, and we should probably try to find a way to throw a better error.
If you override equals in your custom migration class with
@Override
public boolean equals(Object o) {
return o instanceof MyMigration;
}
It should work.
We could also remove the migration field from the configuration class, but that introduce other chances of bugs. What do you think @beeender ?
@cmelchior It worked for me ;)
It would be great if migrations could be defined in assets folder by providing something like that:
<schema_version>.realm
ALTER TABLE Items ADD COLUMN color INTEGER;
@fernandodev I have created #1977 for future discussions of your idea. It could be an interesting idea for future development - especially if the way to specify migrations could be reused across the platforms/languages as Realm supports. I'm closing this issue as it sounds like you have found a solution.
I had this problem when initializing a special Realm configuration for my test cases. What happened was that the Applications's realm configuration was set and created before the @Before method on a Junit test was run, therefore when the test created a new config, it crashed.
- I ran into this error when calling the migration with the
contextof aGcmTaskService(and not having implementedequals/hashcodeon Realm-java 1.2.0 - Is it possible that using the correct
Contextis important?
So, instead of:
RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).....
whereas context might be App.this or GcmTaskService.this, one should always do a context.getApplicationContext():
RealmConfiguration realmConfig = new RealmConfiguration.Builder(context.getApplicationContext()).....
We only keep a weak reference to the Context, so if you use anything but the application context you risk the RealmConfiguration suddenly throwing a null pointer. So yes, it does matter.
We have documented in the Javadoc as unfortunately the Android API's does not allow us to verify that you actually parsed in the application context.
Can't you do something like context.getApplicationContext() on a context you receive in JNI interface to make sure its app context (before passing it further)? Seems too easy... then it comes to RealmConfiguration realmConfig = new RealmConfiguration.Builder(context.getApplicationContext()) this Builder is a part of plain java code inside that Builder.class, isn't it? So it seems like you could do context.getApplicationContext() in a Builder before storing it in the WeakRef
@stan-ko you probably missed the fact that Realm 2.x branch has a Realm.init(Context) call that calls context.getApplicationContext() and stores it for life.
Although I do wonder how well that works with multiple processes. Afaik you have to call Realm.init(application) for both process's application instance
But yes, to Realm 1.2.0 and lower, you can just give the builder context.getApplicationContext()
@Zhuinden It is indeed going to be interesting to see how we call init() when we introduce multi-process support.
Not sure I see the problem with Realm.init(). Each process have their own heap, so each will also have a copy of whatever Realm.init() is doing. The Application class is instantiated for each process as well. If I remember correctly, it is only ContentProviders that are created once in the main process.
@cmelchior if there is a different applicationContext for each process, then it is all OK 👍