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

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

Open akm0012 opened this issue 10 years ago • 17 comments

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

akm0012 avatar Nov 13 '16 04:11 akm0012

I also updated to 2.2.0 and everything is still working.

FabianTerhorst avatar Nov 13 '16 10:11 FabianTerhorst

Do you by chance use Kotlin or have multiple project modules?

Do you have instant run disabled?

Zhuinden avatar Nov 13 '16 10:11 Zhuinden

Almost same problem, but with com.android.tools.build:gradle:2.3.0-alpha1 + dataBinding { enabled = true } Realm plugin not generate classes and app crash with error "is not part of the schema for this Realm". Without dataBinding + 2.3.0 - everything works. Gradle 2.2.2 + databinding works too.

Quarx2k avatar Nov 13 '16 11:11 Quarx2k

Not using Kotlin, I have a Dev and Prod flavor, but don't think that should affect anything.

akm0012 avatar Nov 13 '16 16:11 akm0012

I believe this might be related to the fix with support for annotationProcessor in https://github.com/realm/realm-java/pull/3754 and also to https://github.com/realm/realm-java/issues/3781

Do you use android-apt plugin?

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

apply plugin: 'com.neenbedankt.android-apt'

If you don't, and you don't use Kotlin, then can you try?

Zhuinden avatar Nov 13 '16 17:11 Zhuinden

(but if you do use Kotlin, then you mustn't add android-apt: https://github.com/realm/realm-java/issues/3786 )

Zhuinden avatar Nov 13 '16 23:11 Zhuinden

@akm0012 Can you post your build.gradle files?

cmelchior avatar Nov 14 '16 09:11 cmelchior

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        // Used for Firebase
        classpath 'com.google.gms:google-services:3.0.0'

        // Used in Realm
        classpath "io.realm:realm-gradle-plugin:2.2.0"

        // Used in Butter knife
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenCentral()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        applicationId "removed"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "0.0.1"

        buildConfigField "String", "TEST_RESOURCES_PATH", "\"" + getProjectDir().toURI() + "\"";
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    productFlavors {
        Dev {}
        Prod {}
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support:support-v4:25.0.0'
    compile 'com.android.support:support-v13:25.0.0'
    compile 'com.android.support:recyclerview-v7:25.0.0'
    compile 'com.android.support:cardview-v7:25.0.0'
    compile 'com.google.firebase:firebase-crash:9.8.0'
    compile 'com.google.firebase:firebase-core:9.8.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.3.1'
    compile 'com.google.code.gson:gson:2.4'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    compile 'com.jakewharton:butterknife:8.4.0'
    testCompile 'junit:junit:4.12'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.3.1'
    }
}

// Used for Firebase
apply plugin: 'com.google.gms.google-services'

akm0012 avatar Nov 15 '16 03:11 akm0012

Now I'm getting this error while trying to write to Realm:

com.plzjoinus.plzjoinus.Network.Models.RealmObjects.AccessToken cannot be cast to io.realm.AccessTokenRealmProxyInterface

May need a new ticket. Going to downgrade to 2.1.1 until Realm is a little more stable again.

akm0012 avatar Nov 15 '16 03:11 akm0012

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

Zhuinden avatar Nov 15 '16 08:11 Zhuinden

I got the same problem. Did not want to open a new issue. classpath 'com.android.tools.build:gradle:2.3.0-alpha1' classpath "io.realm:realm-gradle-plugin:2.2.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath 'com.google.gms:google-services:3.0.0'

and I'm using data binding. also #3781

ghost avatar Nov 16 '16 14:11 ghost

@harisvsulaiman @akm0012 Please try the order of apply as mentioned above. If that solves the issue, we'll document it :-)

kneth avatar Nov 21 '16 08:11 kneth

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

dkej123 avatar Nov 21 '16 09:11 dkej123

I close the issue and refer to #3827.

kneth avatar Nov 22 '16 08:11 kneth

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

akm0012 avatar Jan 31 '17 16:01 akm0012

Just upgraded to 2.3.0 also and rearranging the plugins fixed it

lockeb avatar Jan 31 '17 16:01 lockeb

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.

erodewald avatar Nov 03 '17 22:11 erodewald