Android 上的领域 - 如何通过 ID 列表 (@PrimaryKey) select 多个对象?
Realm on Android - How to select multiple objects by list of ids (@PrimaryKey)?
我正在使用 Realm 数据库构建一个 Android 应用程序。
我有一个名为 Article
的 RealmObject
子类,它有一个 id
字段(它和 int
还有一个 @PrimaryKey
)。我想将文章 ID 的 int
s(Set
、int[]
或其他)列表传递给查询,并仅检索这些文章。
在SQL中是这样的:
SELECT *
FROM `table`
where ID in (5263, 5625, 5628, 5621)
我在 的 iOS 中看到可以这样做。
如何在 Android 中执行此操作?谢谢!
编辑: 特此通知,我也在 GitHub 回购 here.
上问过这个问题
Realm Java API 还不支持,很遗憾。您可以在此处关注功能请求 https://github.com/realm/realm-java/issues/841
当前的解决方法是在 for 循环中自己构建查询:
RealmResults<Article> articles = realm.allObjects(Article.class);
RealmQuery q = articles.where();
for (int id : ids) {
q = q.equalTo("id", id);
}
RealmResults<Article> filteredArticles = q.findAll();
更新:
Realm 1.2.0 添加了 RealmQuery.in()
用于与多个值进行比较。 The documentation details all the available overloads. This one 是我们可以使用的方法,如果我们的 id 是 Integer
s:
public RealmQuery<E> in(String fieldName, Integer[] values)
原回答:
来自@ChristianMelchior 的回答 return 如果 id 列表为空,则所有文章。我希望它 return 为空 RealmResults<Article>
。这就是我最终所做的:
Set<Integer> articleIds = this.getArticleIds();
RealmQuery<Article> query = realm.where(Article.class);
if (articleIds.size() == 0) {
// We want to return an empty list if the list of ids is empty.
// Just use alwaysFalse
query = query.alwaysFalse();
} else {
int i = 0;
for (int id : articleIds) {
// The or() operator requires left hand and right hand elements.
// If articleIds had only one element then it would crash with
// "Missing right-hand side of OR"
if (i++ > 0) {
query = query.or();
}
query = query.equalTo("id", id);
}
}
return query.findAll();
我刚看到这个 post,我想我可以为此投入 2 美分。尽管我很欣赏 Christian Melchior 和他的回答,但我认为在这种情况下他的回答不起作用(至少在当前版本中)。
我更喜欢这样做——我个人认为它比 Albert Vila 的回答更具可读性:
List<String> listOfIds = [..];
RealmQuery<SomeClass> query = realm.where(SomeClass.class);
boolean first = true;
for (String id : listOfIds) {
if (!first) {
query.or();
} else {
first = false;
}
query.equalTo("id", id);
}
RealmResults<SomeClass> results = query.findAll();
现在 realm v 1.2.0 支持 RealmQuery.in()
对多个值进行比较。
自 1.2.0 以来,Realm 就是这样做的:
public RealmQuery<E> in(String fieldName, String[] values) {
if (values == null || values.length == 0) {
throw new IllegalArgumentException(EMPTY_VALUES);
}
beginGroup().equalTo(fieldName, values[0]);
for (int i = 1; i < values.length; i++) {
or().equalTo(fieldName, values[i]);
}
return endGroup();
}
之前我是这样做的
我正在使用 Realm 数据库构建一个 Android 应用程序。
我有一个名为 Article
的 RealmObject
子类,它有一个 id
字段(它和 int
还有一个 @PrimaryKey
)。我想将文章 ID 的 int
s(Set
、int[]
或其他)列表传递给查询,并仅检索这些文章。
在SQL中是这样的:
SELECT *
FROM `table`
where ID in (5263, 5625, 5628, 5621)
我在
如何在 Android 中执行此操作?谢谢!
编辑: 特此通知,我也在 GitHub 回购 here.
上问过这个问题Realm Java API 还不支持,很遗憾。您可以在此处关注功能请求 https://github.com/realm/realm-java/issues/841
当前的解决方法是在 for 循环中自己构建查询:
RealmResults<Article> articles = realm.allObjects(Article.class);
RealmQuery q = articles.where();
for (int id : ids) {
q = q.equalTo("id", id);
}
RealmResults<Article> filteredArticles = q.findAll();
更新:
Realm 1.2.0 添加了 RealmQuery.in()
用于与多个值进行比较。 The documentation details all the available overloads. This one 是我们可以使用的方法,如果我们的 id 是 Integer
s:
public RealmQuery<E> in(String fieldName, Integer[] values)
原回答:
来自@ChristianMelchior 的回答 return 如果 id 列表为空,则所有文章。我希望它 return 为空 RealmResults<Article>
。这就是我最终所做的:
Set<Integer> articleIds = this.getArticleIds();
RealmQuery<Article> query = realm.where(Article.class);
if (articleIds.size() == 0) {
// We want to return an empty list if the list of ids is empty.
// Just use alwaysFalse
query = query.alwaysFalse();
} else {
int i = 0;
for (int id : articleIds) {
// The or() operator requires left hand and right hand elements.
// If articleIds had only one element then it would crash with
// "Missing right-hand side of OR"
if (i++ > 0) {
query = query.or();
}
query = query.equalTo("id", id);
}
}
return query.findAll();
我刚看到这个 post,我想我可以为此投入 2 美分。尽管我很欣赏 Christian Melchior 和他的回答,但我认为在这种情况下他的回答不起作用(至少在当前版本中)。
我更喜欢这样做——我个人认为它比 Albert Vila 的回答更具可读性:
List<String> listOfIds = [..];
RealmQuery<SomeClass> query = realm.where(SomeClass.class);
boolean first = true;
for (String id : listOfIds) {
if (!first) {
query.or();
} else {
first = false;
}
query.equalTo("id", id);
}
RealmResults<SomeClass> results = query.findAll();
现在 realm v 1.2.0 支持 RealmQuery.in()
对多个值进行比较。
自 1.2.0 以来,Realm 就是这样做的:
public RealmQuery<E> in(String fieldName, String[] values) {
if (values == null || values.length == 0) {
throw new IllegalArgumentException(EMPTY_VALUES);
}
beginGroup().equalTo(fieldName, values[0]);
for (int i = 1; i < values.length; i++) {
or().equalTo(fieldName, values[i]);
}
return endGroup();
}
之前