【问题标题】:RealmResults to RealmListRealmResults 到 RealmList
【发布时间】:2016-04-10 20:16:01
【问题描述】:

我正在开发一个商店应用程序,每个用户都可以在其中销售商品,也可以将其他用户销售的商品添加到他的愿望清单中。

现在在用户个人资料中,我想显示他添加到愿望清单中的所有项目。

我的班级和关系是:

项目{ ID, 卖家ID, ...(其他属性) }

用户{ 用户身份, ...(其他属性) }

希望{ 用户身份, 项目 ID }

现在的问题是,为了获得我需要做的愿望清单项目: 1. 获取给定用户 ID 的愿望清单。 2. foreach愿望清单项目,根据Wish->ItemId属性从数据库中获取项目。

    RealmList<Item> items = new RealmList<>();
    RealmResults<Wish> wishes= realm.where(Wish.class).equalTo("UserId", userId).findAll();
    for(Wish w : wishes)
    {
       Item item = realm.where(Item.class).equalTo("ItemId", w.getItemId).findfirst();
       items.add(item);
    }

问题是这给了我领域列表,但我需要 RealmResults

有什么建议吗?

【问题讨论】:

  • items.where().findAll() 会给你一个 RealmResults,但是你有什么理由需要一个 RealmResults?

标签: android realm realm-list


【解决方案1】:

您可以使用此解决方法:

    RealmQuery<Item> query = realm.where(Item.class);
    RealmResults<Wish> wishes= realm.where(Wish.class).equalTo("UserId", userId).findAll();
    for(Wish w : wishes)
    {
       query.equalTo(”itemId”, w.getItemId()).or()
    }
    query.equalTo(”itemId”, Constants.DEFAULT); //this is a must for the right hand side ”or” rule.

    RealmResults<Item> items = query.findAll();

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 2019-06-13
    • 2015-07-18
    • 1970-01-01
    • 2016-01-09
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多