Realm Query to fetch multiple rows based on date Android

1.2k Views Asked by At

I have a Realm database with data in it. I need to fetch multiple rows from the database based on a given date. I used the following query and it does not return me the required results.

 RealmResults<GenericTrait> result = realm.where(GenericTrait.class)
                .equalTo("date", MainActivity.date)
                .equalTo("id","Openness")
                .or()
                .equalTo("id","Neuroticism")
                .findAll();

I need to fetch the rows for id's of Openness, Neuroticism for the given date. How should I do it. Please help.

1

There are 1 best solutions below

0
On

In Realm AND is implicit while OR is explicit. So currently your query would read like this:

(date = MainActivity.date AND id = Openness) OR id = Neuroticism

You can manually set beginGroup/endGroup, so your query should instead be this:

RealmResults<GenericTrait> result = realm.where(GenericTrait.class)
        .equalTo("date", MainActivity.date)
        .beginGroup()
            .equalTo("id","Openness")
            .or()
            .equalTo("id","Neuroticism")
        .endGroup()
        .findAll();

Which will give you the correct grouping:

date = MainActivity.date AND (id = Openness OR id = Neuroticism)