Realm query with List

后端 未结 4 1423
-上瘾入骨i
-上瘾入骨i 2020-12-19 01:37

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 da

4条回答
  •  伪装坚强ぢ
    2020-12-19 02:24

    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 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();
    

提交回复
热议问题