Adding standalone-objects to a RealmList

最后都变了- 提交于 2019-12-03 12:55:45
Christian Melchior

Christian from Realm here. No, currently that is not possible. It is an interesting use case though. The reason we have a Realm.copyToRealm() method is to make it really explicit that you should no longer use your old object. Allowing to add standalone objects to already persisted lists would make that less transparent. You also still need it to happen inside a write transaction. Adding a reference to the Realm in your method call would probably be the best way to solve it.

//In Activity
realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        Contact contact = realm.where(Contact.class)
                               .equalTo("name","pete")
                               .findFirst();
        if(contact != null) {
            UpdateHelper.update(contact, realm);
        }
    }
});

//helper method
public static void update(Contact contact, Realm realm) {
    //do update stuff
    Email email = realm.copyToRealm(new Email());
    contact.getEmails().add(email);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!