Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm query with List

Tags:

android

realm

I'm using realm to store my data on Android. Awesome framework! Now the only problem I'm now having is:

I got a array list strings with id's of Countries in my database.

Now I retrieve my Drinks that contains a relationship to countries.

Is there a way that I could to do a query like this:

String [] ids;

realm.where(Drinks.class).equalsTo("country.id", ids);

Something like that?

Or do I really need to do a query to get me all drinks and then filter the list manually?

EDIT:

My classes:

public class Drinks extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;
    private Country country;
}

public class Country extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;
}
like image 512
user1007522 Avatar asked Aug 18 '15 07:08

user1007522


3 Answers

In latest version of Realm 7+, you can use anyOf to match a field against a list of values.

anyOf("name", new String[]{"Jill", "William", "Trillian"})

in older versions, use in instead of anyOf and with kotlin use oneOf instead of in.

see this issue

like image 55
All Іѕ Vаиітy Avatar answered Nov 17 '22 13:11

All Іѕ Vаиітy


Since the Realm database has added RealmQuery.in() with the version 1.2.0

I suggest using something like this.

//Drinks
public class Drinks extends RealmObject {
@PrimaryKey
private String id;
private String name;
private String countryId;

//getter and setter methods
}

//Country
public class Country extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;

//getter and setter methods
}

The code to use inside activity/fragments to retrieve drink list

String[] countryIdArray = new String[] {"1","2","3"} //your string array
RealmQuery<Drinks> realmQuery  = realm.where(Drinks.class)
            .in("countryId",countryIdArray);
RealmResults<Drinks> drinkList = realmQuery.findAll();
like image 37
Sagar Chapagain Avatar answered Nov 17 '22 13:11

Sagar Chapagain


What you want to do is possible with link queries in theory (searching for "country.id"), however link queries are slow. Also you'd need to concatenate a bunch of or() predicates together, and I would not risk that with a link query.

I would recommend using the following

public class Drinks extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;
    private Country country;
    @Index
    private String countryId;
}

public class Country extends RealmObject {
    @PrimaryKey
    private String id;
    private String name;
}

And when you set the Country in your class, you also set the countryId as country.getId().

Once you do that, you can construct such:

RealmQuery<Drinks> drinkQuery = realm.where(Drinks.class);
int i = 0;
for(String id : ids) {
    if(i != 0) {
        drinkQuery = drinkQuery.or();
    }
    drinkQuery = drinkQuery.equalTo("countryId", id);
    i++;
}
return drinkQuery.findAll();
like image 10
EpicPandaForce Avatar answered Nov 17 '22 14:11

EpicPandaForce