realm-java: "Object" is not part of the schema for this Realm after upgrading from 2.1.1 to 2.2.0

Goal

I want to upgrade to Realm 2.2.0.

Expected Results

All my Realm Objects should work the same way as they did in 2.1.1.

Actual Results

When I try to do a simple Query:

 public AccessToken getAccessToken() {
        return realm.where(AccessToken.class).equalTo(AccessToken.PRIMARY_KEY, AccessToken.PRIMARY_KEY_VALUE).findFirst();
    }

I get this Crash:

11-12 23:37:15.172 26922-26922/com.plzjoinus.plzjoinus E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.plzjoinus.plzjoinus, PID: 26922
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.plzjoinus.plzjoinus/com.plzjoinus.plzjoinus.Activities.LoginActivity}: java.lang.IllegalArgumentException: AccessToken is not part of the schema for this Realm
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                          Caused by: java.lang.IllegalArgumentException: AccessToken is not part of the schema for this Realm
                                                                             at io.realm.internal.modules.CompositeMediator.getMediator(CompositeMediator.java:172)
                                                                             at io.realm.internal.modules.CompositeMediator.getTableName(CompositeMediator.java:90)
                                                                             at io.realm.RealmSchema.getTable(RealmSchema.java:296)
                                                                             at io.realm.RealmSchema.getSchemaForClass(RealmSchema.java:316)
                                                                             at io.realm.RealmQuery.<init>(RealmQuery.java:138)
                                                                             at io.realm.RealmQuery.createQuery(RealmQuery.java:87)
                                                                             at io.realm.Realm.where(Realm.java:1213)
                                                                             at com.plzjoinus.plzjoinus.DataManagers.DAO.checkIfExists(DAO.java:225)
                                                                             at com.plzjoinus.plzjoinus.Activities.LoginActivity.onCreate(LoginActivity.java:47)
                                                                             at android.app.Activity.performCreate(Activity.java:6664)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                             at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Steps & Code to Reproduce

  1. Upgrade to 2.2.0
  2. Attempt to access RealmObjects
  3. Get the above Crash.

If I downgrade to 2.1.1, it works.

I have tried cleaning the project and reinstalling the app with no success.

Code Sample

Where this is crashing:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        // Check to see if we have an AccessToken
        if (DAO.getInstance().getAccessToken() != null) {
            startActivity(new Intent(this, AddGroupActivity.class));
        }
    }

My DAO Object:

public class DAO {

    public static final String TAG = "Realm";

    private static final String REALM_NAME = "plzJoinUs.realm";

    private static final int REALM_SCHEMA_VERSION = 0;

    private static DAO instance;

    private Realm realm;

    // --------------------------------------
    // ----------- Realm Functions ----------
    // --------------------------------------

    /**
     * Initializes Realm.
     *
     * @param context The Application Context.
     */
    public static void init(@NonNull Context context) {
        Realm.init(context);
        Realm.setDefaultConfiguration(getRealmConfiguration());
    }

    public DAO() {
        realm = Realm.getDefaultInstance();
    }

    public static DAO getInstance() {
        if (instance == null) {
            instance = new DAO();
        } else {
            if (instance.realm.isClosed()) {
                instance = new DAO();
            }
        }

        return instance;
    }

    /**
     * Gets a RealmConfiguration. This is where we set the Realm name and version.
     *
     * @return The RealmConfiguration.
     */
    private static RealmConfiguration getRealmConfiguration() {
        return new RealmConfiguration.Builder()
                .name(DAO.REALM_NAME)
                .schemaVersion(DAO.REALM_SCHEMA_VERSION)
                .deleteRealmIfMigrationNeeded()
                .build();
    }

    /**
     * Used to Close the Realm.
     * <p>
     * Should be done in onDestroy of all Activities.
     */
    public void close() {
        Log.d(TAG, "Attempting to close Realm.");

        if (realm.isInTransaction()) {
            Log.e(TAG, "Realm is in a Transaction!  Having to Cancel Transaction");
            realm.cancelTransaction();
        }

        if (!realm.isClosed()) {
            Log.d(TAG, "Closing Realm.");
            realm.close();
        }
    }

    // --------------------------------------
    // ------------- Access Token -----------
    // --------------------------------------

    /**
     * Saves the AccessToken to Realm.
     *
     * @param accessToken The access token we want to save.
     * @param realmListener The Listener that will get hit once the write is completed.
     */
    public void saveAccessToken(@NonNull AccessToken accessToken, @Nullable RealmListener realmListener) {
        writeOrUpdateRealmObject(accessToken, realmListener);
    }

    /**
     * Gets the AccessToken from Realm.
     *
     * @return The AccessToken object.
     */
    public AccessToken getAccessToken() {
        return realm.where(AccessToken.class).equalTo(AccessToken.PRIMARY_KEY, AccessToken.PRIMARY_KEY_VALUE).findFirst();

}

Here is my RealmModel:

package com.plzjoinus.plzjoinus.Network.Models.RealmObjects;

import io.realm.RealmObject;
import io.realm.annotations.Ignore;
import io.realm.annotations.PrimaryKey;

/**
 * The Access Token used in making API calls.
 */
public class AccessToken extends RealmObject {

    @Ignore
    private static final String TOKEN_TYPE = "Bearer";

    // We do this because we only want one AccessToken in the Database.
    @Ignore
    public static final String PRIMARY_KEY_VALUE = "ONE_TOKEN_TO_RULE_THEM_ALL";

    @Ignore
    public static final String PRIMARY_KEY = "id";

    @PrimaryKey
    private String id = PRIMARY_KEY_VALUE;

    private String accessToken;

    private String organizerId;

    @Ignore
    private String tokenType;

    // --------------------------------
    // ------ Getters and Setters -----
    // --------------------------------

    public String getBearerToken() {
        return TOKEN_TYPE + " " + accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public String getOrganizerId() {
        return organizerId;
    }

    public void setOrganizerId(String organizerId) {
        this.organizerId = organizerId;
    }
}

Version of Realm and tooling

Realm version(s): 2.1.1 -> 2.2.0

Realm sync feature enabled: no (Not sure really, so probably no)

Android Studio version: 2.2.2

Which Android version and device: Nexus 6P, 7.0

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@akm0012 Try applying plugins in this order:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

Just upgraded to 2.3.0 also and rearranging the plugins fixed it

@kneth Hi, I had the same problem with Realm like guys above. Reordering plugins as @Zhuinden suggest helped me. It works with gradle 2.2.2 and realm 2.2.0.

@kneth : So just upgraded to 2.3.0 and was seeing the same issue, however, after rearranging the order of the “apply plugin: …” things seem to be fixed. Thanks!

After applying kotlin plugins I had this error because I added them after realm-android. Keeping realm at the end of the plugins solved it.

I also updated to 2.2.0 and everything is still working.