Search code examples
androidrealm

RealmQuery vs RealmResult


what are the differences between RealmQueryand a RealmResult?

More concretely:

On a RealmQueryI can filter with equalTo, but I have to pass the properties name as a String, which is error-prone.
A RealmResult is a List, so I can filter with the filter method, which is way more convenient, because I have acces to the properties.

Are there any performance or memory impacts, if always do;

Java Code:

realm.where(realmModel.class).findAll().filter(...)

and don't use the RealmQuerys equalTo() and in() methods?

Thank you


Solution

  • realm.where(realmModel.class).findAll().filter(...)
    

    That filter() comes from List<T> interface, meaning you are not actually executing your query inside the Realm database. You're just running a filter() for every single element that the findAll() returns, which is every element, instead of an actual filtered list that is filtered by Realm (database-side).

    That, and I think that filter(new Predicate() {... is only available API 24+.

    If you don't want to use strings for field names, then use constants instead.

    Are there any performance or memory impacts, if always do;

    Yes.

    In SQL terms, this would be equivalent to SELECT * FROM TABLE and filtering every single element by hand in memory, instead of SELECT * FROM TABLE WHERE ... -- except in the Realm world.