RealmResults 到 RealmList

RealmResults to RealmList

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

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

我的 类 和关系是:

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

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

愿望{ 用户身份, 商品编号 }

现在的问题是,为了获得我需要做的愿望清单项目: 1. 获取给定用户 ID 的愿望清单。 2. foreach wish-list item,根据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);
    }

问题是这给了我 realmlist,但我需要 RealmResults

有什么建议吗?

您可以使用此解决方法:

    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();