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

Compound Pojo (all members are not simple data type) and io.realm.exceptions.RealmException: Could not find the generated proxy class error

Open sam-ghosh opened this issue 11 years ago • 6 comments

My questions

  1. Is it possible to use a class (extends RealmObject) that has a non-simple member variable? Please see example below
  2. Not sure if this , or anything else is causing the Could Not find generated proxy class error. Why would there be a proxy class, I can't see any annotations anywhere

I have the following Pojo:

public class Feed {

    int id;
    public String text;
    public String time_created;
    String time_modified;
    int comments_count;
    int likes_count;
    String feed_type;
    int obj_id;
    String image;
    String user_name;
    String user_earthmile_points;
    boolean liked;
    boolean commented;
    boolean is_private;
    String url;
    int feed_creator_id;
}

import io.realm.RealmObject;

public class RealmFeed extends RealmObject{
    Feed obj;

    public Feed getObj() {
        return obj;
    }

    public void setObj(Feed obj) {
        this.obj = obj;
    }
}

And I am doing the following

public void writeNewFeedstoDatabase(ArrayList<Feed> newsFeedList){
    Realm realm = Realm.getInstance(this.getActivity());            // getActivity() to be called after onAttach in fragment, called here in onActivityCreated, so should be fine

    for (Feed feed:newsFeedList){
        realm.beginTransaction();    // Error =  io.realm.exceptions.RealmException: Could not find the generated proxy class
        RealmFeed f = realm.createObject(RealmFeed.class);
        f.setObj(feed);
        realm.commitTransaction();

    }

Full Error

Process: com.example.somghosh.testandroid, PID: 32321
io.realm.exceptions.RealmException: Could not find the generated proxy class
        at io.realm.Realm.createObject(Realm.java:211)
        at com.example.somghosh.testandroid.pager.ScreenNewsHome.writeNewFeedstoDatabase(ScreenNewsHome.java:95)
        at com.example.somghosh.testandroid.pager.ScreenNewsHome.consumeApiData(ScreenNewsHome.java:84)
        at com.example.somghosh.testandroid.pager.ScreenNewsHome.access$200(ScreenNewsHome.java:33)
        at com.example.somghosh.testandroid.pager.ScreenNewsHome$1.success(ScreenNewsHome.java:65)
        at com.example.somghosh.testandroid.pager.ScreenNewsHome$1.success(ScreenNewsHome.java:62)
        at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

sam-ghosh avatar Dec 23 '14 20:12 sam-ghosh

From their documentation:

Realm supports the following field types: boolean, short, ìnt, long, float, double, String, Date and byte[]. The integer types short, int, and long are all mapped to the same type (long actually) within the realm. Moreover, subclasses of RealmObject and RealmList<? extends RealmObject> are supported to model relationships.

So, the answer is no, you can't do it. Hope that helps. :)

dotgc avatar Dec 23 '14 20:12 dotgc

Thanks @ImGauravC I made it this way because I am using the first class public class Feed with Retrofit and GSON to parse a JSON response from server and put it nicely into Pojo objects.

I tried to use public class Feed extends RealmObject (and get rid of this 2-level class as I have in my original post), however GSON goes into an infinite loop for some reason (gives a StackOverFlow error) #696

sam-ghosh avatar Dec 23 '14 20:12 sam-ghosh

I think it leaves me with the only option of creating another RealmFeed class with the exact same variables as Feed class. It is not the best solution, but would work in this case

sam-ghosh avatar Dec 23 '14 20:12 sam-ghosh

I'm also using Retrofit + okhttp + GSON together with realm. Because one is not allowed to create a new Foo extends RealmObjects the default way I also had to workaround it. But as long as there are no lists or nested objects needed, like in your example, one POJO works for both GSON and realm for me:

public class Feed extends RealmObject {
  private int id;
  private String text;
  @Ignore
  private int numberInJsonButToBeIgnoredByRealm;

  // getters & setters
}
Feed gsonFeed = getFeedByRetrofit();
copyToRealm(gsonFeed);
public static copyToRealm(Feed gsonFeed) {
  realm.beginTransaction();
  Feed realmFeed = realm.createObject(Feed.class); 
  // the ugly part starts here
  realmFeed.setId(gsonFeed.getId());
  // ...
}

When there are Lists or nested Objects I used to also use one POJO but with a list workaround: https://github.com/realm/realm-java/issues/620#issuecomment-66640786

Realm now supports JSON, maybe you can drop GSON therefore?

an-k avatar Dec 24 '14 02:12 an-k

Re 2): The annotation is inherited from the RealmObject (so you don't have to write any). Is your issue resolved now?

bmunkholm avatar Dec 25 '14 16:12 bmunkholm

@somghosh I assume you found a workaround for now. Please reopen if that is not the case.

cmelchior avatar Jan 05 '15 10:01 cmelchior