realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

migration quesstion

Open onlynight opened this issue 10 years ago • 17 comments

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

onlynight avatar Dec 09 '15 09:12 onlynight

Can you post how you are creating the configuration as well as getting the Realm instance?

cmelchior avatar Dec 09 '15 10:12 cmelchior

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

FishOSU avatar Dec 10 '15 15:12 FishOSU

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.

corcoran avatar Dec 10 '15 20:12 corcoran

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).

cmelchior avatar Dec 10 '15 20:12 cmelchior

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 avatar Dec 10 '15 21:12 corcoran

@corcoran Thanks for the detailed report!

kneth avatar Dec 11 '15 10:12 kneth

@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 avatar Dec 17 '15 07:12 cmelchior

@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 avatar Dec 21 '15 11:12 fernandodev

@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.

kneth avatar Dec 21 '15 12:12 kneth

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.

jturolla avatar Mar 09 '16 15:03 jturolla

  • I ran into this error when calling the migration with the context of a GcmTaskService (and not having implemented equals/hashcode on Realm-java 1.2.0
  • Is it possible that using the correct Context is 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()).....

weitzj avatar Sep 01 '16 17:09 weitzj

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.

cmelchior avatar Sep 01 '16 17:09 cmelchior

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 avatar Dec 07 '16 10:12 stan-ko

@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 avatar Dec 07 '16 10:12 Zhuinden

@Zhuinden It is indeed going to be interesting to see how we call init() when we introduce multi-process support.

kneth avatar Dec 07 '16 12:12 kneth

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 avatar Dec 07 '16 12:12 cmelchior

@cmelchior if there is a different applicationContext for each process, then it is all OK 👍

Zhuinden avatar Dec 07 '16 13:12 Zhuinden