Realm-java: Support for where in RealmList query

Created on 6 Apr 2015  Â·  9Comments  Â·  Source: realm/realm-java

I couldn't find the way to write a query like this

realm.where(Product.class).in(catId,"categories").findAll();

//Product categories 
private RealmList<RealmInt> categories;

Am I missing something?

T-Help

Most helpful comment

Just a quick note, ran into this issue when implementing the in predicate by @kneth in the second comment of this thread.

Using it exactly as written, it threw:

UnsupportedOperationException: Missing left-hand side of OR

I had to account for the first condition before the OR in the loop, something like:

RealmQuery<Product> query = realm.where(Product.class);
// ids is a list of the category ids
if (ids.size() > 0) {
   query = query.equalTo("categories.id", ids.get(0));
   for (int i = 1; i < ids.size(); i++) {
      query = query.or().equalTo("categories.id", ids.get(i));
   }
}

I know, trivial, but I figured I would comment just in case someone else ran into the same issue.

All 9 comments

It is true that Realm doesn't have an in predicate - a similar method is proposed in #841. Currently you have to manually build the query. For instance, if you wish to find all Products which belong to certain categories, you can do:

RealmQuery<Product> query = realm.where(Product.class);
// categoryList is a list of the categories
for(int catId : categoryList) {
    query = query.or().equalTo("categories.id", catId);
}
RealmResults<Product> results = query.findAll();

As you can see, the in predicate is trivial to implement, but we haven't given it any priority yet.

Ok. In fact in query in relational database does not match exactly here. In sql you give the list and ask is one of the list elements match column value? But here My modal has a list field and i say give me the modal objects whose list field contains my catID(int) ? Some syntax like that

catId is an int primitive variable
categories is list of int field belongs to Product modal
realm.where(Product.class).where().contains("categories",catId).findAll();

I can solve it with iterating list and checking each element. Thats ok. But my real problem is here http://stackoverflow.com/questions/29493101/gson-deserialization-for-realm-list-of-primitives
currently?

Thanks.

We do have an open issue for primitive types in RealmList (see #575) which is basically what you need, isn't it?

Your contains() in your snippet must be rewritten using or().equalTo() in a loop (iterating over categories) for the time being. At some point we should implement something like in() or contains().

Ohh .At last, I reached my aim. Problem with gson is resolved by an answer from my stackoverflow question http://stackoverflow.com/questions/29493101/gson-deserialization-for-realm-list-of-primitives/29512817#29512817

list iterating is resolved with your suggestion like that

realm.beginTransaction();
RealmResults<Product> realmProducts = realm.where(Product.class).equalTo("categories.val",catId).findAll();
realm.commitTransaction();
return realmProducts;

This issue can be closed..

@Kml55 Happy to hear that you got it to work.

You really don't need the transaction in the code snippet above - they are only used when you change (create, update, delete an object) your Realm.

Just a quick note, ran into this issue when implementing the in predicate by @kneth in the second comment of this thread.

Using it exactly as written, it threw:

UnsupportedOperationException: Missing left-hand side of OR

I had to account for the first condition before the OR in the loop, something like:

RealmQuery<Product> query = realm.where(Product.class);
// ids is a list of the category ids
if (ids.size() > 0) {
   query = query.equalTo("categories.id", ids.get(0));
   for (int i = 1; i < ids.size(); i++) {
      query = query.or().equalTo("categories.id", ids.get(i));
   }
}

I know, trivial, but I figured I would comment just in case someone else ran into the same issue.

@nathanws You are right: or() is very strict in the sense that it requires both a left and right predicate while the implicit and operator doesn't. We decided to go with implicit and operator as we believe that and is much more common than or.

public class Post extends RealmObject {
private RealmList categories;
private int author;
}
I used gson.

I tried to query like this
RealmQuery<Post> query = realm.where(Post.class); for(int catId : categoryList) { query = query.or().equalTo(ModelConstant.categories, catId); } RealmResults<Post> results = query.findAll();
No luck I also tried this way
realmResults.where().equalTo(ModelConstant.categories, category_id).findAll();
Anyone to help?

@Sadmansamee Please open a new issue or ask at Stackoverflow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CNyezi picture CNyezi  Â·  3Comments

jjorian picture jjorian  Â·  3Comments

yuwu picture yuwu  Â·  3Comments

mithrann picture mithrann  Â·  3Comments

harshvishu picture harshvishu  Â·  3Comments