bailando bailando
bailando bailando

Reputation: 2341

RealmResults to RealmList

I am developing a shop application, where each user can sell items and can also add items that other users sells to his wish-list.

Now in the user profile, i want to display all of the items that he added to his wish-list.

My classes & relations are:

Item{ Id, sellerId, ...(other properties) }

User{ userId, ...(other propeties) }

Wish{ UserId, ItemId }

now the problem is, that in order to get the wish-list items i need to do: 1. get the wish-list list for the given user id. 2. foreach wish-list item, get the item from the database based on the Wish->ItemId property.

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

problem is that this gives me realmlist, but i need RealmResults

Any advice?

Upvotes: 1

Views: 682

Answers (1)

Hay Zohar
Hay Zohar

Reputation: 238

You can use this walkaround:

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

Upvotes: 1

Related Questions