Search code examples
androidrealm

`User` is not part of the schema for this Realm - How to find out whether 'User' exists?


Say I wanna know whether User is empty.

This is my code:

public boolean isEmpty() {
    Realm myRealm = Realm.getInstance(this);
    User results1 = myRealm.where(User.class).findFirst();
    return results1 == null;
}

If it's the first time using the app, I need to know that, because if that's the case, I need to ask the user to fill the form. (It's not really authentication, just a simple form).

In this situation, how can I know whether User exists without having to create a record.

Edit:

Here's the stack:

FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ga.xuser/com.example.ga.xuser.View.Activities.Profile}: java.lang.IllegalArgumentException: User is not part of the schema for this Realm
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      at android.app.ActivityThread.access$600(ActivityThread.java:141)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)

Solution

  • The cause - absent schema migration. You need to add new objects to existing database schema when you create new RealmModel class (User in your case). Or you can remove database and in that case Realm will create new schema which will contain all of you models.

    Migration

    You can found very clean sample in Migrations paragraph of ducementation. If more details required, see also migration sample app on GitHub.

    P.S. If keyword this in expession Realm.getInstance(this) - it's an instance of Context, you use super old Realm version. Realm.getInstance(Context) was removed in 0.83.0 if I remember correctly. I siggest to update to latest version of Realm (cerrent 3.2.1).