Saving realm relationships does not persist them

前端 未结 2 1152
悲哀的现实
悲哀的现实 2021-01-22 18:29

I have a list of articles.
Those articles are inserted by using realm.copyToRealmOrUpdate(); which works perfectly fine.

Now each article has an autho

相关标签:
2条回答
  • 2021-01-22 19:03

    Are you using GSON? If so, did you follow the example as outlined in http://realm.io/docs/java/0.80.0/#gson?

    0 讨论(0)
  • 2021-01-22 19:21

    Christian from Realm here. You keep manipulating the standalone objects you just copied to Realm, which won't work as they are still standalone objects and not "proper" Realm objects. You can read more here: http://realm.io/docs/java/0.80.0/#creating-objects and in the JavaDoc http://realm.io/docs/java/0.80.0/api/io/realm/Realm.html#copyToRealm-E-

    So if you change you code to something like the below, it should work:

    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            List<Article> realmArticles = realm.copyToRealmOrUpdate(articles.data);
    
            for(Article article : realmArticles) {
                Author author = realm.where(Author.class).equalTo("id", article.getSerializedAuthor()).findFirst();
                article.setAuthor(author);
    
                for (Image image : article.getSerializedImages().data) {
                    article.getImages().add(image);
                }
            }
        }
    });
    
    0 讨论(0)
提交回复
热议问题