realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

Realm RealmObject is not part of the schema for this Realm #3139

Closed falecci closed 8 years ago

falecci commented 8 years ago

RealmObject is not part of the schema

I have run into this error everytime I make "Clean and rerun app" in Android Studio. Everything worked fine before.

Expected Results

?

Actual Results

FATAL EXCEPTION: main Process: com.lt. PID: 11965 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lt.com.lt.ui.activities.MainActivity}: io.realm.exceptions.RealmException: class com.lt.model.Order is not part of the schema for this Realm. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.access$800(ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) Caused by: io.realm.exceptions.RealmException: class com.lt.model.Order is not part of the schema for this Realm. at io.realm.internal.RealmProxyMediator.getMissingProxyClassException(RealmProxyMediator.java:217) at io.realm.DefaultRealmModuleMediator.getTableName(DefaultRealmModuleMediator.java:72) at io.realm.RealmSchema.getSchemaForClass(RealmSchema.java:235) at io.realm.RealmQuery.(RealmQuery.java:136) at io.realm.RealmQuery.createQuery(RealmQuery.java:85) at io.realm.Realm.where(Realm.java:1026) at com.lt.ui.activities.MainActivity.onCreate(MainActivity.java:85) at android.app.Activity.performCreate(Activity.java:6010) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)  at android.app.ActivityThread.access$800(ActivityThread.java:155)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5343)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)

Steps & Code to Reproduce

This is when I start up the application, I need to know if it exists an order unfinished. I have tried these things already.

1.Clean and rebuild project. 2.Deleted generated build files manually. 3.Uninstall app before deploying. 4.I have realm in my project gradle and app gradle. (version 1.1.0) 5.I have tried with Order extending RealmObject and implementing RealmModel. 6.RealmConfiguration has deleteRealmIfMigrationNeeded.

Code Sample


         RealmConfiguration realmConfig = new RealmConfiguration
            .Builder(getApplicationContext())
            .deleteRealmIfMigrationNeeded()
            .name("myRealmFile")
            .build();

        Realm.setDefaultConfiguration(realmConfig);            

        Realm realm = Realm.getDefaultInstance();

        RealmResults<Order> orders = realm.where(Order.class).equalTo("finished", false).findAll();

        mHasActiveOrder = orders != null && orders.isValid() && orders.size() > 0;

        realm.close();

Version of Realm and tooling

Realm version(s): 1.1.0

Android Studio version: 2.1.2

Which Android version and device: API LVL 24 with Android Moto G with API LVL 22.

Zhuinden commented 8 years ago

My guess goes for that you should disable Instant Run in Android Studio.

If you're implementing RealmModel, then if I know correctly you also need to add the @RealmClass annotation. However, it's worth noting that this should work out of the box if you're extending RealmObject, so I think instant run is messing things up.

Should be File -> Settings -> Build Execution Deployment -> Instant Run and you should make sure Enable instant run is unticked.

zaki50 commented 8 years ago

Hi @falecci

Are you using InstantRun? If yes, please turn it off.

5.I have tried with Order extending RealmObject and implementing RealmModel. no need to do both. please remove implements RealmModel.

rshah commented 8 years ago

I have something similar. Please let me know if i should open new issue or continue here.

io.realm.exceptions.RealmMigrationNeededException: The MyUserRealm class is missing from the schema for this Realm.

This is my setup: Android Studio: 2.1.2 Language: Kotlin 1.0.3 Realm: 1.0.1 Gradle plugin: 2.1.2 Gradle wrapper version: 2.14 Android target API Level: 24 Android compile SDK: 24 Build tool version: 24.0.0 Instant Run: Disabled Testing device running: Android 4.3 Apk is release mode debugable.

First install normal, but the second build will reproduce the error.

If you need more information please let me know.

Zhuinden commented 8 years ago

@rshah can you try updating to 1.1.0? there were some bugfixes between the two regarding the "optional API" and some other things..

stk1m1 commented 8 years ago

@rshah

Actually, I think you should file another issue with detailed crash log. It seems we need more detail on your case to support you. Thanks!

rshah commented 8 years ago

I created new issue here: https://github.com/realm/realm-java/issues/3159

Zhuinden commented 8 years ago

Actually now that you mention it, @falecci and @rshah can you try the following from https://github.com/realm/realm-java/issues/3196 for Kotlin-related schema classes missing:

Try running apply plugin: 'realm-android' after apply plugin: 'kotlin-android' (and extensions)

Also try adding to build.gradle

android {
    dexOptions {
        incremental false
    }
}
stk1m1 commented 8 years ago

Hi @falecci

Could you update us on whether the comments above have helped you?

rshah commented 8 years ago

In my case the plugin order already like above, but incremental true . And seems after i converted all realm classes to kotlin. The problem did not occured anymore. I will try incremental false if the problem appear again

luca992 commented 8 years ago

same issue. io.realm.exceptions.RealmException: class *.*.*.*.RealmAccount is not part of the schema for this Realm.

I've tried running apply plugin: 'realm-android' after apply plugin: 'kotlin-android' (and extensions). And I've set incremental to false and also am running apply plugin: 'realm-android' after apply plugin: 'kotlin-android' (and extensions).

Cleaned and reran many times. Invalidating cache and restarting doesn't help.

Even tried updating to the 1.1.1 snapshot

luca992 commented 8 years ago

Actually I've figured out that I needed RealmModules, as half of my realm objects are inside a separate module that we share between two apps. But the weird thing is that that realm objects inside the shared module were added to the schema for the realm, but the realm objects in the main app module were excluded from the schema.

Zhuinden commented 8 years ago

Actually I've figured out that I needed RealmModules, as half of my realm objects are inside a separate module that we share between two apps.

Yes, if you use separate modules and library projects, then you need to expose the models in the library as RealmModules.

kneth commented 8 years ago

@luca992 Did you try to have separate RealmModules for libraries?

luca992 commented 8 years ago

@kneth yes, that got it working for me.

kneth commented 8 years ago

@luca992 Great to hear. I'll close the issue.

JuniorSfeir commented 7 years ago

This might also happen when using proguard, as you need to give directives to keep the realm classes to be used

Zhuinden commented 7 years ago

Ah yes, if you don't add the default proguard, then you need to specify that keep * extends io.realm.RealmObject or something like that

luna-vulpo commented 7 years ago

Hi, I ran in same problem. I use: kotlin, dagger2 and realm. So my gradle file looks like this:

apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'

    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
        incremental = false
    }
    dependencies {
       compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
       annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
       provided 'javax.annotation:jsr250-api:1.0'
    }

And using such configuration I can compile the project. But get error that class is not part of the schema for this Realm. What I miss?

Zhuinden commented 7 years ago

@luna-vulpo that you should use kapt instead of annotationProcessor, and that you should execute kotlin-android before realm-android

zaki50 commented 7 years ago

Working example which uses Realm, Kotlin, Dagger2 and DataBinding https://github.com/zaki50/RealmKotlin_template

developer-- commented 7 years ago

I had same problem see this it may helpfull http://stackoverflow.com/questions/42947367/object-is-not-part-of-the-schema-for-this-realm?noredirect=1#comment72992816_42947367

edman commented 7 years ago

For the record, I am using realm successfully in a multi-project android build without RealmModule.

But then for some reason the "RealmObject is not part of the schema" problem only surfaced during instrumentation tests.

Subclassing the android application class for instrumentation tests and using a configuration with RealmModules in it solved the problem.

Zhuinden commented 7 years ago

@edman there is a chance you will run into trouble without RealmModules in 3.5.0+.

You can check with 3.5.0-SNAPSHOT if what I said is true.

edman commented 7 years ago

@Zhuinden true, I got an error with 3.5.0-SNAPSHOT and it was resolved with RealmModules.

The exception was more cryptic than the one I had in version 3.4.0. For the record:

java.lang.AbstractMethodError: abstract method "java.util.Map io.realm.internal.RealmProxyMediator.getExpectedObjectSchemaInfoMap()"
    at io.realm.Realm.initializeRealm(Realm.java:468)
    at io.realm.Realm.createAndValidateFromCache(Realm.java:423)
    at io.realm.Realm.createInstance(Realm.java:371)
    at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:346)
    at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284)
    at io.realm.Realm.getDefaultInstance(Realm.java:281)
AllenWen commented 6 years ago

@Zhuinden Thanks man, for solving my problem

@luna-vulpo that you should use kapt instead of annotationProcessor, and that you should execute kotlin-android before realm-android

Zhuinden commented 6 years ago

Generally it should look like this

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
Vaishnavi-optisol commented 6 years ago

Even after changing in above order I'm getting same issue @Zhuinden

Zhuinden commented 6 years ago

@Vaishnavi-optisol Unfortunately i do not know enough about your project to tell you what's wrong

The order above generally worked for everyone

You can try https://realm.io/docs/java/latest/#how-do-i-customize-dependecies-defined-by-the-realm-gradle-plugin

Karikari commented 4 years ago

Check your Class Definition Make sure you have extended your class to RealmObject. and don't implement RealmModule