FVod
FVod

Reputation: 2295

Realm do a IN query in android

Is is possible to do a "IN" query with Realm in android? I mean, doing the equivalent of "SELECT X FROM X WHERE x IN (...)?

Thank you very much in advance!

Upvotes: 4

Views: 1753

Answers (2)

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

In java:

RealmResults dogs = realm
             .where(Dog.class)
             .in("name", new String[] {"Larry","Curly","Moe"})
             .findAll();

In kotlin (note that in is a keyword so, check the syntax changes):

val dogs = realm
     .where<Dog>()
     .`in`("name", arrayOf("Larry", "Curly", "Moe"))

Or

val dogs = realm
     .where<Dog>()
     .anyOf("age", arrayOf("Larry", "Curly", "Moe"))

Upvotes: 0

EpicPandaForce
EpicPandaForce

Reputation: 81539

The official IN operator (since 1.2.0) works like this:

public RealmQuery<E> in(String fieldName, String[] values) {
    if (values == null || values.length == 0) {
        throw new IllegalArgumentException(EMPTY_VALUES);
    }
    beginGroup().equalTo(fieldName, values[0]);
    for (int i = 1; i < values.length; i++) {
        or().equalTo(fieldName, values[i]);
    }
    return endGroup();
}

Upvotes: 3

Related Questions