Usage of parceler (@Parcel) with Realm.io (Android)
Asked Answered
S

2

4

I have the following code which produces the error: Error:Parceler: Unable to find read/write generator for type io.realm.Realm for io.realm.RealmObject.realm

It was working all fine without extends RealmObject , however I want to use Realm to put to database easily. Is there a way to exlcude the RealmObject fields and just use the basic pojo fields for @Parcel?

@Parcel
public class Feed extends RealmObject{
    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;

   }
Saury answered 23/12, 2014 at 13:56 Comment(2)
See github.com/johncarl81/parceler/issues/57Repletion
Technically, I have never seen a use-case in which it makes sense to parcel a RealmObject. You're supposed to only send primary keys.Checkerbloom
T
14

EDIT #2: Actually, I found a way to make it work :). See the updated answer below.

EDIT #1: While the app compiles just fine, it crashes when trying to actually create a Parcel with the error: org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy. The Realm team has officially acknowledged that it is currently not possible to implement Parcelable on RealmObjects. It is unclear if / when this will be resolved.


With Parceler v0.2.16, you can do this:

@RealmClass      // required if using JDK 1.6 (unrelated to Parceler issue)
@Parcel(value = Parcel.Serialization.BEAN, analyze = { Feed.class })
public class Feed extends RealmObject {
    // ...
}

Then, use Parcels.wrap(Feed.class, feed) instead of Parcels.wrap(feed) everywhere, otherwise your app will crash with org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy.

Turbosupercharger answered 17/4, 2015 at 2:16 Comment(2)
Will it use Serialization? I've heard serialization is 10x slower than parcel. I don't want to compromise on performance. So should I use this technique or use some other work around?Unchaste
@Unchaste No. Parceler will still create standard Android Parcels, you don't need to worry about that. That's its whole job :) (To be very clear actually, even creating a Parcel is a form of "serialization", so it is not correct to say "serialization is 10x slower than parcel". Instead you should say, "using the Java Serializable interface is slower than creating a Parcel", and the reason for that is, Serializable creates lots of temporary objects which causes a lot of GC).Turbosupercharger
Q
1

All classes that extend RealmObject will have a matching RealmProxy class created by the annotation processor. Parceler must be made aware of this class. Note that the class is not available until the project has been compiled at least once.

@Parcel(implementations = { PersonRealmProxy.class },
    value = Parcel.Serialization.BEAN,
    analyze = { Person.class })
public class Person extends RealmObject {
// ...}
Qadi answered 5/12, 2016 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.