New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(Question) How to select multiple objects by id (@PrimaryKey)? #1757
Comments
Unfortunately, Realm java currently has no support for this so you have to build up an OR query manually to achieve the same thing. RealmQuery<MyRealmObject> query = realm.where(MyRealmObject.class);
int i = 0;
for (int id : ids) {
if (i++ > 0)
query = query.or();
query = query.equalsTo("id", id);
} query.beginGroup(); and query.endGroup(); can be used around the for loop if you need to select on some other parameters too. |
@AlbertVilaCalvo As @knighty said, Realm Java does not support The code for building the query can be a bit shorter.
IN operator issue is tracked as #841 and also #1280 is helpful. I'll close this issue as a duplicate of #841. If you have further questions, feel free to reopen this or create new issue. |
Thank you all. Because I want to return an empty 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 basket is empty. Just search for an id that
// does not exist.
query = query.equalTo("id", -30000);
} 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 ("Missing right-hand side of OR")
if (i++ > 0) {
query = query.or();
}
query = query.equalTo("id", id);
}
} |
Hi. I have a
RealmObject
subclass calledArticle
which has anid
field (it's andint
and also a@PrimaryKey
). I would like to pass to a query a list ofint
s (aSet<>
,int[]
, or whatever) of article id's and retrieve only those articles.In SQL would be like this:
I've seen it's possible to do this in iOS in this StackOverflow question.
How can I do this in Android? Thanks!
The text was updated successfully, but these errors were encountered: