【问题标题】:Usage of parceler (@Parcel) with Realm.io (Android)使用 Realm.io (Android) 使用 parceler (@Parcel)
【发布时间】:2015-02-21 16:23:12
【问题描述】:

我有以下产生错误的代码:Error:Parceler: Unable to find read/write generator for type io.realm.Realm for io.realm.RealmObject.realm

没有 extends RealmObject 一切正常,但是我想使用 Realm 轻松放入数据库。有没有办法排除 RealmObject 字段而只使用 @Parcel 的基本 pojo 字段?

@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;

   }

【问题讨论】:

标签: android parcelable serializable realm parceler


【解决方案1】:

编辑#2:实际上,我找到了一种让它工作的方法:)。请参阅下面的更新答案。

编辑 #1:虽然应用编译得很好,但在尝试实际创建 Parcel 时崩溃,错误为:org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy。 Realm 团队有 officially acknowledged,目前无法在 RealmObjects 上实现 Parcelable。目前尚不清楚是否/何时会解决此问题。


使用Parceler v0.2.16,您可以这样做:

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

然后,在任何地方都使用Parcels.wrap(Feed.class, feed) 而不是Parcels.wrap(feed),否则您的应用会因org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy 而崩溃。

【讨论】:

  • 会使用序列化吗?我听说序列化比包裹慢 10 倍。我不想在性能上妥协。那么我应该使用这种技术还是使用其他解决方法?
  • @penduDev 不会。Parceler 仍然会创建标准的 Android Parcels,您无需担心。这就是它的全部工作:)(实际上要非常清楚,即使创建Parcel 也是一种“序列化”形式,因此说“序列化比包裹慢 10 倍”是不正确的。相反,您应该说,“使用Java 的Serializable 接口比创建Parcel" 慢,that 的原因是Serializable creates lots of temporary objects which causes a lot of GC)。
【解决方案2】:

所有扩展 RealmObject 的类都会有一个由注解处理器创建的匹配 RealmProxy 类。 Parceler 必须知道这个类。请注意,在项目至少编译一次之前,该类不可用。

@Parcel(implementations = { PersonRealmProxy.class },
    value = Parcel.Serialization.BEAN,
    analyze = { Person.class })
public class Person extends RealmObject {
// ...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多