Realm-java: 'value' is not a valid managed object.

Created on 27 Sep 2018  路  4Comments  路  Source: realm/realm-java

Hello,
I tried to save data on the object in realm following official documentation

 private void saveToDataBase(final RealmList<SortedEpg>sortedEpgRealmList) {
        // Get a Realm instance for this thread
        Realm realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                Data data=bgRealm.createObject(Data.class);
                data.setSortedEpgArrayList(sortedEpgRealmList);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
               Log.e("dataBase","<<<<<<<<<<<< stored Data >>>>>>>>>");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Log.e("dataBase",error.getMessage());
            }
        });
    }

And I have Many to many realm object

public class Data extends RealmObject implements Serializable {
    RealmList<SortedEpg> sortedEpgArrayList;

    public Data() {
    }

    public Data(RealmList<SortedEpg> sortedEpgArrayList) {
        this.sortedEpgArrayList = sortedEpgArrayList;
    }

    public void setSortedEpgArrayList(RealmList<SortedEpg> sortedEpgArrayList) {
        this.sortedEpgArrayList = sortedEpgArrayList;
    }

    public RealmList<SortedEpg> getSortedEpgArrayList() {
        return sortedEpgArrayList;
    }
}

and

public class SortedEpg extends RealmObject implements Serializable {
    String CategoryName;
    RealmList<RecyclerDate> recyclerDateRealmList;

    public SortedEpg() {
    }

    public SortedEpg(String CategoryName, RealmList<RecyclerDate> dateArrayList) {
        this.CategoryName = CategoryName;
        this.recyclerDateRealmList = dateArrayList;
    }


    public String getCategoryName() {
        return CategoryName;
    }

    public RealmList<RecyclerDate> getRecyclerDateRealmList() {
        return recyclerDateRealmList;
    }

}

and the level of the model class goes on.
Thanks for the help!

O-Community T-Help

Most helpful comment

 data.setSortedEpgRealmList(sortedEpgRealmList);

You cannot put an unmanaged RealmList into a managed object.

You might be able to do something like this instead, which will convert the unmanaged items to managed objects.

List< SortedEpg> managedList = bgRealm.copyToRealm(sortedEpgRealmList);
data.getSortedEpgRealmList().addAll(managedList);

All 4 comments

RealmList<SortedEpg> sortedEpgArrayList; that's a funny name considering it's clearly not an ArrayList, is it?

Anyways... this means that you can only insert managed RealmObjects into a managed RealmList that you obtained from a managed object.

Solution: make sure you don't insert unmanaged objects into managed RealmList.

You also cannot set a new RealmList over an existing managed RealmList. You have to use Collections.addAll() to add "every item" from the other list.

@Zhuinden I am new to the realm. One of my pojo is also used by retrofit. Do I have to manually add all the items?
Here's my all pojo

public class Data extends RealmObject {
    RealmList<SortedEpg> sortedEpgRealmList;

    public Data() {
    }

    public Data(RealmList<SortedEpg> sortedEpgRealmList) {
        this.sortedEpgRealmList = sortedEpgRealmList;
    }

    public void setSortedEpgRealmList(RealmList<SortedEpg> sortedEpgRealmList) {
        this.sortedEpgRealmList = sortedEpgRealmList;
    }

    public RealmList<SortedEpg> getSortedEpgRealmList() {
        return sortedEpgRealmList;
    }
}
public class SortedEpg extends RealmObject {
    String CategoryName;
    RealmList<RecyclerDate> recyclerDateRealmList;

    public SortedEpg() {
    }

    public SortedEpg(String CategoryName, RealmList<RecyclerDate> recyclerDateRealmList) {
        this.CategoryName = CategoryName;
        this.recyclerDateRealmList = recyclerDateRealmList;
    }


    public String getCategoryName() {
        return CategoryName;
    }

    public RealmList<RecyclerDate> getRecyclerDateRealmList() {
        return recyclerDateRealmList;
    }

}



md5-729c43b42f994b25ef7cf7ad3c1d99cb



public class RecyclerDate extends RealmObject {
    String recyclerDate;
    RealmList<ChannelItem> channelItemRealmList;

    public RecyclerDate(){

    }

    public RecyclerDate(String recyclerDate, RealmList<ChannelItem> channelItemRealmList) {
        this.recyclerDate = recyclerDate;
        this.channelItemRealmList = channelItemRealmList;
    }

    public String getRecyclerDate() {
        return recyclerDate;
    }

    public RealmList<ChannelItem> getChannelItemRealmList() {
        return channelItemRealmList;
    }


}



md5-729c43b42f994b25ef7cf7ad3c1d99cb



public class ChannelItem extends RealmObject  {
    String channelName;
    String logo;
    RealmList<Program> programRealmList;

    public ChannelItem(){

    }

    public ChannelItem(String channelName, String logo, RealmList<Program> programRealmList) {
        this.channelName = channelName;
        this.logo = logo;
        this.programRealmList = programRealmList;
    }

    public String getChannelName() {
        return channelName;
    }

    public String getLogo() {
        return logo;
    }

    public RealmList<Program> getProgramRealmList() {
        return programRealmList;
    }
}



md5-729c43b42f994b25ef7cf7ad3c1d99cb



public class Program extends RealmObject {

    @SerializedName("program_name")
    @Expose
    private String programName;
    @SerializedName("begin_time")
    @Expose
    private String beginTime;
    @SerializedName("end_time")
    @Expose
    private String endTime;
    @SerializedName("short_description")
    @Expose
    private String shortDescription;
    @SerializedName("duration")
    @Expose
    private String duration;

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getProgramName() {
        return programName;
    }

    public void setProgramName(String programName) {
        this.programName = programName;
    }

    public String getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(String beginTime) {
        this.beginTime = beginTime;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

}



md5-3aacdefe7ec6ec4b23c1e1d2c3a49356



private void saveToDataBase(final RealmList<SortedEpg>sortedEpgRealmList) {
        // Get a Realm instance for this thread
        Realm realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm bgRealm) {
                Data data=bgRealm.createObject(Data.class);
                data.setSortedEpgRealmList(sortedEpgRealmList);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
               Log.e("dataBase","<<<<<<<<<<<< stored Data >>>>>>>>>");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Log.e("dataBase",error.getMessage());
            }
        });
    }

Could you please help

 data.setSortedEpgRealmList(sortedEpgRealmList);

You cannot put an unmanaged RealmList into a managed object.

You might be able to do something like this instead, which will convert the unmanaged items to managed objects.

List< SortedEpg> managedList = bgRealm.copyToRealm(sortedEpgRealmList);
data.getSortedEpgRealmList().addAll(managedList);

Thank you @cmelchior for simplifying. It worked perfectly

Was this page helpful?
0 / 5 - 0 ratings