i need to implement the Parcelable interface in a Realm model but i have no idea how to write a RealmList in a Parcel
here is my code:
public class SomeModel extends RealmObject implements Parcelable {
public long id;
public RealmList<SomeOtherModel> otherModels;
protected SomeModel(Parcel in) {
id = in.readLong();
//here i need to otherModel RealmList from parcel
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(id);
// here i need to write the otherModels RealmList
}
//other methods omitted
}
pd: the SomeOtherModel class also implements the Parcelable interface and extends RealmObject
I think it's not possible to make RealmObject parcelable. But if I understand your case right, you can use copyFromRealm() method to detach object from Realm and do whatever you want with it.
Actually you can
Firstly we are talking about un-managed entities.
Then you need to initialize realmList with
new RealmList<>()
. Then add data to it as collection withaddAll
method.That's it! Here is a sample:Person.java:
Dog.java:
I hope that may help,'.
You won't be able to easily make a RealmList parcelable, you should either convert it to JSON and parcel it as a string, or use
Parceler
library with this gist. I think JSON with your custom adapter is actually more reliable, though.EDIT: with Kotlin, you can follow this approach.