I need to convert one of my model objects (which was automatically populated from Json using Retrofit) to a Realm object.
At first, my code was new RealmPoll() instead of realm.createObject(RealmPoll.class) . (I was getting a NullPointerException like this question ). So I solved this problem. But I can not find a way to copy RealmList.
I cannot find examples of creating RealmObjects using RealmLists in the official website in Realm and docs say
Only Realm can create managed RealmLists. Managed RealmLists will automatically update the content whenever the core is updated and can only be accessed using the receiver of the RealmObject.
what makes me think it's somehow impossible? But this is a really simple task. I do not know how to interpret the meaning of the documents.
Is it possible to simply convert an object (e.g. RetrofitPoll below) to a realm object (e.g. RealmPoll below) if it contains a list?
One function that illustrates my question:
private RealmPoll convertRetrofitPollToRealmPoll(Realm realm, RetrofitPoll retrofitPoll) { RealmPoll realmPoll = realm.createObject(RealmPoll.class); //<----- fixed, used to be "new RealmPoll()". //Convert List<Answer> RealmList<RealmAnswer> realmAnswers = new RealmList<RealmAnswer>(); //<----- How to do same thing here? for(RetrofitAnswer retrofitAnswer : retrofitPoll.getAnswers()) { realmAnswers.add(convertRetrofitAnswerToRealmAnswer(retrofitAnswer)); } realmPoll.setAnswers(realmAnswers); }
RetrofitPoll.java
public class RetrofitPoll { private List<Answer> answers; private String id; private Date startDate; private String title; private Topic topic; }
RealmPoll.java
public class Poll extends RealmObject { private RealmList<Answer> answers; private String id; private Date startDate; private String title; private Topic topic; }
source share