Last active
August 3, 2018 01:38
-
-
Save Rexee/3ec92b759b3a944d4ad4b28666b2479c to your computer and use it in GitHub Desktop.
Parceler and RealmList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Parcel(implementations = { CountryRealmProxy.class }, | |
value = Parcel.Serialization.FIELD, | |
analyze = { Country.class }) | |
public class Country extends RealmObject { | |
@ParcelPropertyConverter(RealmListParcelConverter.class) | |
public RealmList<RealmString> languages; | |
} | |
public class RealmListParcelConverter | |
implements TypeRangeParcelConverter<RealmList<? extends RealmObject>, | |
RealmList<? extends RealmObject>> { | |
private static final int NULL = -1; | |
@Override | |
public void toParcel(RealmList<? extends RealmObject> input, Parcel parcel) { | |
parcel.writeInt(input == null ? NULL : input.size()); | |
if (input != null) { | |
for (RealmObject item : input) { | |
parcel.writeParcelable(Parcels.wrap(item), 0); | |
} | |
} | |
} | |
@Override | |
public RealmList fromParcel(Parcel parcel) { | |
int size = parcel.readInt(); | |
RealmList list = new RealmList(); | |
for (int i=0; i<size; i++) { | |
Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader()); | |
list.add((RealmObject) Parcels.unwrap(parcelable)); | |
} | |
return list; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment