境界与查询名单 [英] Realm query with List
问题描述
我使用的境界来存储我在Android上的数据。真棒框架!现在,我现在有唯一的问题是:
我在我的数据库得到了与国家ID的一个数组列表的字符串。
现在我找回我的饮料包含对国家的关系。
有没有一种方法,我可以做这样的查询:
的String [] IDS;realm.where(Drinks.class).equalsTo(country.id,IDS);
类似的东西?
或者我真的需要做一个查询,让我所有的饮料,然后手动过滤列表?
编辑:
我的类:
公共类饮料扩展RealmObject {
@首要的关键
私人字符串ID;
私人字符串名称;
私人国家的国家;
}公共类国家扩展RealmObject {
@首要的关键
私人字符串ID;
私人字符串名称;
}
你想要做什么是可能的链接查询理论(搜索country.id
),但是链接查询很慢。你想也需要连接一堆或()
predicates在一起,我不会冒这个险的链接查询。
我会建议使用以下
公共类饮料扩展RealmObject {
@首要的关键
私人字符串ID;
私人字符串名称;
私人国家的国家;
私人字符串countryId;
}公共类国家扩展RealmObject {
@首要的关键
私人字符串ID;
私人字符串名称;
}
当你设置国家
在你的类,你还可以设置 countryId
为 country.getId()
。
一旦你这样做,你可以构建这样:
RealmQuery<饮料和GT; drinkQuery = realm.where(Drinks.class);
INT I = 0;
对于(字符串ID:IDS){
如果(ⅰ!= 0){
drinkQuery = drinkQuery.or();
}
drinkQuery = drinkQuery.equalTo(countryId,身份证);
我++;
}
返回drinkQuery.findAll();
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;
}
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;
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();
这篇关于境界与查询名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!