Realm where not contains in Java
Asked Answered
C

2

7

If I have an array of primitive values, how can I run a .not().contains() on a .where() from RealmResults ?

The code would hopefully look like this:

 results.where().not().contains("id", new int[] {1, 2, 3})

Or do I have to iterate over all of these results and pluck them out individually?

Compagnie answered 15/6, 2016 at 21:35 Comment(2)
Did you find a solution my dude?Aeroplane
The only solution I found was the answer I have just accepted, unfortunatelyCompagnie
R
2

There is no such method to query against the arrays as of now . The second paramater of contains() requires a string so you cant pass int[] or int . Iterating over the result is the only option .

RealmQuery q = users.where();
for (int id : ids) {
q = q.notEqualsTo("id", id);
}
RealmResults<users> users = q.findAll();

You can use between() if you need to query against a range of value .

Refugia answered 16/6, 2016 at 2:55 Comment(0)
S
3

You can try beginGroup and then not in to query against the Arrays

realm1.where(UserModel.class)..beginGroup().not().
                    in("key",Your Array)).endGroup().findAll();
Subglacial answered 2/2, 2018 at 9:28 Comment(1)
it doesn't need beginGroup()Whop
R
2

There is no such method to query against the arrays as of now . The second paramater of contains() requires a string so you cant pass int[] or int . Iterating over the result is the only option .

RealmQuery q = users.where();
for (int id : ids) {
q = q.notEqualsTo("id", id);
}
RealmResults<users> users = q.findAll();

You can use between() if you need to query against a range of value .

Refugia answered 16/6, 2016 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.