Skip to content
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

Closed
AlbertVilaCalvo opened this issue Nov 11, 2015 · 3 comments
Closed
Labels

Comments

@AlbertVilaCalvo
Copy link

Hi. I have a RealmObject subclass called Article which has an id field (it's and int and also a @PrimaryKey). I would like to pass to a query a list of ints (a Set<>, int[], or whatever) of article id's and retrieve only those articles.

In SQL would be like this:

SELECT *
FROM `table`
where ID in (5263, 5625, 5628, 5621) 

I've seen it's possible to do this in iOS in this StackOverflow question.

How can I do this in Android? Thanks!

@knighty
Copy link

knighty commented Nov 11, 2015

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.

@zaki50
Copy link
Contributor

zaki50 commented Nov 11, 2015

@AlbertVilaCalvo As @knighty said, Realm Java does not support IN query.

The code for building the query can be a bit shorter.

RealmQuery<MyRealmObject> query = realm.where(MyRealmObject.class);
for (int id : ids) {
        query = query.or().equalsTo("id", id);
}

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.

@zaki50 zaki50 closed this as completed Nov 11, 2015
@zaki50 zaki50 removed the Pending label Nov 11, 2015
@AlbertVilaCalvo
Copy link
Author

Thank you all. Because I want to return an empty RealmResults<Article> if the list of ids is empty, that's what I ended up doing:

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants