节能领域的关系不会持续到 [英] Saving realm relationships does not persist them

查看:106
本文介绍了节能领域的关系不会持续到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的<一个列表href="https://github.com/MetalMatze/Krautreporter/blob/8d4e0e0d0d2056cce41f5c461ea7216bf47cba60/app/src/main/java/de/metalmatze/krautreporter/models/Article.java"相对=nofollow>文章的。照片 这些文章是通过使用插入 realm.copyToRealmOrUpdate(); 这工作完全正常

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

现在每篇文章都有哪些不应该坚持的AUTHORID。而我想找到存储的<一个href="https://github.com/MetalMatze/Krautreporter/blob/8d4e0e0d0d2056cce41f5c461ea7216bf47cba60/app/src/main/java/de/metalmatze/krautreporter/models/Author.java"相对=nofollow>作者RealmObject 并设置了文章的关系。

Now each article has an authorId which should not be persisted. Rather I want to find the stored author RealmObject and set its relationship for the article.

Author author = realm.where(Author.class).equalTo("id", article.getSerializedAuthor()).findFirst();
article.setAuthor(author);

不知怎的,这似乎并没有被境界被持久化。

Somehow this does not seem to be persisted by realm.

这同样适用于在<一href="https://github.com/MetalMatze/Krautreporter/blob/8d4e0e0d0d2056cce41f5c461ea7216bf47cba60/app/src/main/java/de/metalmatze/krautreporter/models/Image.java"相对=nofollow>形象RealmObject ,只是我在保存之前迭代。

The same applies to the image RealmObject, just that I iterate before saving.

下面是完整的片段。

realm.beginTransaction();
realm.copyToRealmOrUpdate(articles.data);

for(Article article : articles.data) {
    Author author = realm.where(Author.class).equalTo("id", article.getSerializedAuthor()).findFirst();
    article.setAuthor(author);

    for (Image image : article.getSerializedImages().data) {
        if (article.getImages() == null) {
            article.setImages(new RealmList<Image>());
        }
        article.getImages().add(image);
    }
}
realm.commitTransaction();

请让我知道如果你需要更多的信息。
谢谢你。

Please let me know if you need more information.
Thanks.

推荐答案

从境界这里的基督徒。你一直操纵你刚才复制到域独立的对象,这是行不通的,因为它们仍然是独立的对象,而不是正确的境界对象。你可以点击这里阅读: http://realm.io/docs/java/ 0.80.0 /#创建对象并在JavaDoc的 http://realm.io/docs/java/0.80.0/api/io/realm/Realm.html#copyToRealm-E-

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-

所以,如果你改变你的code到类似下面的,它应该工作:

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

realm.beginTransaction();
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);
    }
}
realm.commitTransaction();

这篇关于节能领域的关系不会持续到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆