Realm on Android - How to select multiple objects by list of ids (@PrimaryKey)?

前端 未结 5 1257
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-18 21:25

I\'m building an Android app with the Realm database.

I have a RealmObject subclass called Article which has an id field (it\'s an

5条回答
  •  既然无缘
    2021-02-18 21:54

    I just came across this post and I thought I could throw in my 2 cents on this. As much as I appreciate Christian Melchior and his answers I think in this case his answer is not working (at least in the current version).

    I prefer to do it like this - I personally think it's more readable than Albert Vila's answer:

    List listOfIds = [..];
    RealmQuery query = realm.where(SomeClass.class);
    
    boolean first = true;
    for (String id : listOfIds) {
        if (!first) {
            query.or();
        } else {
            first = false;
        }
        query.equalTo("id", id);
    }
    RealmResults results = query.findAll();
    

提交回复
热议问题