3

I using Android Studio 1.2.2 and Realm 0.81.1. I have created a Model 'Category' as follows:

@RealmClass
public class Category extends RealmObject {
   private String name;
   // getter and setter
}

But I am getting java.lang.IllegalArgumentException: Category is not part of the schema for this Realm

I even enabled Annotation Processing, but the error is still persisting.

How can I solve this error? Any help is much appreciated.

Update

I dug deeper into the Realm code. I found that in Util.class file,

if(!superclass.equals(RealmObject.class)) {
        clazz = superclass;
    }

It is checking the superclass of the model I am using. When I printed out the super class of the mode; myself, like:

category.getClass().getSuperclass().getName();

I am getting 'io.realm.RealmObject' which is not equal to RealmObject.class. So Realm might not be considering it as a RealmObject.

Could this be the reason that is causing error?

Shivang Doshi
  • 245
  • 1
  • 8
  • 19
  • When do you see the error message? Did you add the `Category` class in a newer version of your app and migrate the Realm file? – geisshirt Jul 10 '15 at 08:17
  • I see the error message on runtime, when I am trying to query the Realm. I didn't understand the second question. My app does not have any other db, creating from scratch with Realm so I don't have to migrate, right? – Shivang Doshi Jul 10 '15 at 17:33
  • 1
    Have you got a chance to figure this out? I got this issue when enabling Proguard. Do you use ButterKnife? – Ralphilius Jul 13 '15 at 18:59
  • @kodingralph no i still havent. and no i dont use ButterKnife. Was it working for u before that ? – Shivang Doshi Jul 15 '15 at 00:08
  • Yes, can you post your gradle console log? – Ralphilius Jul 15 '15 at 02:38
  • 2
    Are you sure you enabled the annotation processing? I am using kotlin for realm. And I got the same problem until I use kapt to process the annotation. – blade Aug 08 '15 at 23:53

4 Answers4

4

In my case, I need to add realm plugin (apply plugin: 'realm-android') to every .gradle project that uses realm. RealRecyclerView and my main app project.

darkwater84
  • 103
  • 6
2

I found that the order in which the plugins are specified in the build.gradle file of your app matters. In my case, I had the apply plugin: 'realm-android' in it but apparently it was specified too early. Putting it last worked.

This failed.

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

Yet this worked (note how real-android is last).

apply plugin: 'com.android.application'
apply plugin: 'android-apt'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'realm-android'
T. Francis
  • 169
  • 1
  • 12
1

I don't know if you still have this issue but on my side I just clear the project and build again. Works fine after that... Please notice that I have android studio as ide.

Lucian Novac
  • 1,255
  • 12
  • 18
0

In my case I had two One-to-Many relationship. Two classes of the relationship were in different modules. This caused error. When I put them in the same module it worked. This is the related part from the source:

    public <E extends RealmModel> E copyOrUpdate(Realm realm, E obj, boolean update, Map<RealmModel, RealmObjectProxy> cache) {
    // This cast is correct because obj is either
    // generated by RealmProxy or the original type extending directly from RealmObject
    @SuppressWarnings("unchecked") Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass());

    if (clazz.equals(Buddy.class)) {
        return clazz.cast(BuddyRealmProxy.copyOrUpdate(realm, (Buddy) obj, update, cache));
    } else {
        throw getMissingProxyClassException(clazz);
    }
}
ulusoyca
  • 841
  • 9
  • 18