0

I want to use @Parcel annotation instead of implementing Parcelable.
So my class is :

@Parcel
public class UserSkillSpec {
    private final String TAG = getClass().getSimpleName();

    private Skill skill;
    private int endorses = 0;
    private boolean endorsed = false;
    private List<User> endorsers = new LinkedList<>();
    private int proficiency = 0;
    private boolean verified = false;

    @ParcelConstructor
    public UserSkillSpec(Skill skill, int proficiency) {
        this.skill = skill;
        this.proficiency = proficiency;
    }

}    

when I want to build project, this error will appears in MessageBox

Error:(62, 109) error: incompatible types: Object cannot be converted to User    

and User source code is :

@Parcel
@RealmClass
public class User extends RealmObject {
    @PrimaryKey
    private String id;

    private String firstName;
    private String lastName;
    private String name;
   //and something else

    public User() {
    }
}    

Apparently this issue related to List.
How can I fix my problem?
Thanks.

EDIT1: I changed User class as below code:

@Parcel(implementations = {UserRealmProxy.class},
        value = Parcel.Serialization.BEAN,
        analyze = {User.class})
@RealmClass
public class User implements RealmModel {
    @PrimaryKey
    private String id;

    private String firstName;
    private String lastName;
    private String name;
    //somethings else

    public User() {
    }
}

in automatically generated class UserSkillSpec$$Parcelable by Parceler library,the error happened in for section :

public static void write(UserSkillSpec userSkillSpec$$1, android.os.Parcel parcel$$1, int flags$$0, IdentityCollection identityMap$$0) {
        int identity$$0 = identityMap$$0 .getKey(userSkillSpec$$1);
        if (identity$$0 != -1) {
            parcel$$1 .writeInt(identity$$0);
        } else {
            parcel$$1 .writeInt(identityMap$$0 .put(userSkillSpec$$1));
            Skill$$Parcelable.write(InjectionUtil.getField(Skill.class, UserSkillSpec.class, userSkillSpec$$1, "skill"), parcel$$1, flags$$0, identityMap$$0);
            parcel$$1 .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$$1, "proficiency"));
            if (InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers") == null) {
                parcel$$1 .writeInt(-1);
            } else {
                parcel$$1 .writeInt(InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers").size());
                for (User user$$0 : InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers")) {
                    User$$Parcelable.write(user$$0, parcel$$1, flags$$0, identityMap$$0);
                }
            }
            parcel$$1 .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$$1, "verified")? 1 : 0));
            parcel$$1 .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$$1, "endorses"));
            parcel$$1 .writeString(InjectionUtil.getField(String.class, UserSkillSpec.class, userSkillSpec$$1, "TAG"));
            parcel$$1 .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$$1, "endorsed")? 1 : 0));
        }
    }
Mojtaba
  • 382
  • 1
  • 3
  • 19

3 Answers3

0

You should probably use implements over extend for your user, because your parent class is not parcelable.

Tips

You should consider to implement your custom creator like this

Timo
  • 497
  • 10
  • 21
0

Finally I could solve my problem,and decide to share it.
Tips: 1. List must be as Generic.List<Item> not List
2. use @ParcelPropertyConverter(ItemListParcelConverter.class) in declaration of List<Item>

so my code is like below now:

@Parcel(implementations = {UserRealmProxy.class},
        value = Parcel.Serialization.BEAN,
        analyze = {User.class})
public class User extends RealmObject {
    @PrimaryKey
    private String id;

    private String firstName;
    private String lastName;
    private String name;}    

and this one:

@Parcel
public class UserSkillSpec {
    private Skill skill;
    private int endorses = 0;
    private boolean endorsed = false;
    @ParcelPropertyConverter(ItemListParcelConverter.class)
    private List<User> endorsers = new LinkedList<>();
    private int proficiency = 0;
    private boolean verified = false;

    public UserSkillSpec() {
    }
}

and this is parcelConvertor class:

public class ItemListParcelConverter<T> extends ArrayListParcelConverter<T> {
    @Override
    public void itemToParcel(T item, Parcel parcel) {
        parcel.writeParcelable(Parcels.wrap(item), 0);
    }

    @Override
    public T itemFromParcel(Parcel parcel) {
        return Parcels.unwrap(parcel.readParcelable(getClass().getClassLoader()));
    }
}
Mojtaba
  • 382
  • 1
  • 3
  • 19
0

If you want to use RealmModel instead of RealmObject you can follow this to working with Parceler library.

Ex Class:

@Parcel(implementations = { CustomAreaRealmProxy.class },
        value = Parcel.Serialization.BEAN,
        analyze = { CustomArea.class })
@RealmClass
public class CustomArea implements RealmModel {
    ...
}

And for your proguard file;

# Parceler library
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
-keepnames public class * extends io.realm.RealmObject
-keepnames public class * implements io.realm.RealmModel  #<- This is the fix
Berkay Turancı
  • 3,373
  • 4
  • 32
  • 45