Realm-java: Can't initiate Realm Configuration in AttachBaseContext in Application Class.

Created on 6 Oct 2017  路  10Comments  路  Source: realm/realm-java

Expected Results

Realm initialized with the RealmConfiguration.

Actual Results

java.lang.IllegalStateException: Call Realm.init(Context) before creating a RealmConfiguration

Steps & Code to Reproduce

Can't set realm configuration in Application class in attachBaseContext. it is possible in OnCreate.

Code Sample


@Override
    protected void attachBaseContext(final Context base) {
        initRealm(base);
}
private void initRealm(Context context) {
        Realm.init(context);

        try {
            RealmConfiguration configuration = new RealmConfiguration.Builder().name(REALM_NAME).schemaVersion(1).deleteRealmIfMigrationNeeded()
                    .build();
            Realm.setDefaultConfiguration(configuration);
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }

    }

Version of Realm and tooling

Realm version(s): ?

3.7.2

Realm sync feature enabled: yes/no

no.

Android Studio version: ?

3.0 b7

Which Android version and device: ?

Emulator, any android version.

T-Bug

Most helpful comment

@cmelchior I assume he needs to have a language set for the user depending on in-app language preferences, and this is stored in Realm, and you have to override the base context of Application to override the language or so.

We didn't run into this problem because we stored this data in shared pref.

All 10 comments

have you tried calling super.attachBaseContext(base) first, then using Realm.init(this)?

I haven't because I need Realm to wrap my base context (according to user preference).

....what? Surely there must be a less hacky way of doing this.

I haven't found a way to change app. language at runtime without wrapping the base context. But anyway, I believe it is a bug because I still can use Realm before attaching, just can't set my configuration.

Well if you check the source code, then you can see that you get this error message if BaseRealm.applicationContext == null, which means that the context you provide to Realm.init(context) has its getApplicationContext() method called, and it returns null.

As for whether getApplicationContext() returning null there is something Realm should prepare for, I don't know!

Worst case scenario you can set that context field manually through package io.realm; by creating a static method there that writes into BaseRealm.applicationContext.

Any reason you need to access the Realm that early in the app's lifecycle? We require the context to be able to find context.getFilesDir() without that, you will not be able to save or open any Realm file.

The reason why is I have a User.class and it has language preference. In my App initialization I do a ContextWrap. Realm works (it opens my db) without problem EVEN before attaching the baseContext. I can initialize Realm and use it, I just can't set a Configuration.

@cmelchior I assume he needs to have a language set for the user depending on in-app language preferences, and this is stored in Realm, and you have to override the base context of Application to override the language or so.

We didn't run into this problem because we stored this data in shared pref.

Looking into this, it seems you are right. The reason we are calling context.getApplicationContext() was original to protect against memory leaks, like people using an Activity for the Context.

However, given the pattern where Realm is normally initialized in the Application subclass, the chance of that happening is really low.

So I don't have a problem with us changing it to just use the Context directly.

Was this page helpful?
0 / 5 - 0 ratings