Search code examples
javaandroidrealm

Android Realm: how to search string is either null or empty


I have a very quick question. How to get all records where the particular field (String type) is not null and empty.

Currently, I do:

String nullStr = null; //temporary null string to pass it to realm.

RealmResults<Feedback> feedbacks = realm.where(Feedback.class)
                .notEqualTo("Comment", nullStr)
                .notEqualTo("Comment", "")
                .findAll();

Is this the only way to get record that is not null and empty? How about if the Comment contains only spaces? Is there a way to get record where the field is not null and not contain white-spaces?

Thanks


Solution

  •   realm.where(Feedback.class)
                .isNull("Comment")
                .or()
                .equalTo("Comment", "")
                .findAll();
    

    And if you want the inverse of this, use not().beginGroup()./*query here*/.endGroup()