How to check realm for android data existence?

Azlan Jamal picture Azlan Jamal · Jun 27, 2016 · Viewed 10.6k times · Source

how do I check whether certain value is exist or not in my realm database based on this code below?

realm.where(User.class).equalTo("cardId", cardId).findFirst()

Thanks in advance.

Answer

Niko picture Niko · Jun 27, 2016

You can perform null check.

User user = realm.where(User.class).equalTo("cardId", cardId).findFirst();

if (user != null) {
    // Exists
} else {
    // Not exist
}