Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new RealmObject that contains a RealmList

Tags:

android

realm

I need to convert one of my model objects (which was automatically filled from Json using Retrofit) to a Realm Object.

At first, my code was new RealmPoll() instead of realm.createObject(RealmPoll.class). (I was getting NullPointerException just like this question) So I solved that issue. But I cannot find a way to copy a RealmList.

I can't find any examples of creating RealmObjects with RealmLists in the official Realm website tutorial and the docs say

Only Realm can create managed RealmLists. Managed RealmLists will automatically update the content whenever the underlying Realm is updated, and can only be accessed using the getter of a RealmObject.

which makes me believe it is somehow not possible? But it is a really simple task. I don't know how to interpret the docs meaning.

Is there any way possible to just convert an object (such as RetrofitPoll below) to a realm object (such as 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;
}
like image 667
Rock Lee Avatar asked Oct 15 '15 22:10

Rock Lee


1 Answers

It should be possible to do the following

ObjectWithList obj = new ObjectWithList();
RealmList<Foo> list = new RealmList();
list.add(new Foo());
obj.setList(list);

realm.beginTransaction();
realm.copyToRealm(obj); // This will do a deep copy of everything
realm.commitTransaction();

If you are using Retrofit to create your entire object-graph, you should be able to copy everything into Realm using just one a one-liner. If not, it is a bug.

Note this is also in the docs:

 * Non-managed RealmLists can be created by the user and can contain both managed and non-managed
 * RealmObjects. This is useful when dealing with JSON deserializers like GSON or other
 * frameworks that inject values into a class. Non-managed elements in this list can be added to a
 * Realm using the {@link Realm#copyToRealm(Iterable)} method.

Non-managed lists are created by just doing new RealmList() but this could probably be clearer in the docs.

like image 59
Christian Melchior Avatar answered Sep 24 '22 00:09

Christian Melchior