我正在使用领域将我的数据存储在
Android上.很棒的框架!现在我唯一的问题是:
我的数据库中有一个带有国家ID的数组列表字符串.
现在我找回包含与国家关系的饮料.
有没有办法可以像这样进行查询:
- String [] ids;
- realm.where(Drinks.class).equalsTo("country.id",ids);
那样的东西?
编辑:
我的课程:
- 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;
- }
解决方法
你理想的是理论上的链接查询(搜索“country.id”),但是链接查询很慢.此外,您需要将一堆或()谓词连接在一起,我不会冒险使用链接查询.
我建议使用以下内容
- 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;
- }
在类中设置Country时,还将countryId设置为country.getId().
一旦你这样做,你可以构建这样的:
- 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();