On this page
The examples in this page use two Realm object types:
See the schema for these two classes below:
This gives you a new instance of the class RealmResults, containing teachers with the name "Ms. Langtree" or "Mrs. Jacobs".
RealmQuery includes several methods that can execute queries:
- findAll() blocks until it finds all objects that meet the query conditions
- findAllAsync() returns immediately and finds all objects that meet the query conditions asynchronously on a background thread
- findFirst() blocks until it finds the first object that meets the query conditions
- findFirstAsync() returns immediately and finds the first object that meets the query conditions asynchronously on a background thread
Queries return a list of references to the matching Realm objects using the RealmResults type.
For example, consider a query for all teachers with a student named "Wirt" or "Greg":
You can even use dot notation to query inverse relationships:
Realm Database applies the
limit() methods in the order you specify. Depending on the
data set this can alter the query result. Generally, you should
limit() last to avoid unintended result sets.
You can define the order of query results using the sort() method:
Sorts organize results in ascending order by default. To organize results
in descending order, pass
Sort.DESCENDING as a second argument.
You can resolve sort order ties between identical property values
by passing an array of properties instead of a single property: in the
event of a tie, Realm Database sorts the tied objects by subsequent
properties in order.
Realm uses non-standard sorting for upper and lowercase
letters, sorting them together rather than sorting uppercase first.
As a result,
'- !"#0&()*,./:;?_+<=>123aAbBcC...xXyYzZ is the
actual sorting order in Realm Database. Additionally, sorting
strings only supports the
Latin Extended A, and
Latin Extended B (UTF-8 range 0–591)
You can cap the number of query results to a specific maximum number using the limit() method:
Limited result collections automatically update like any other query result. Consequently, objects might drop out of the collection as underlying data changes.
Some databases encourage paginating results with limits to avoid reading unnecessary data from disk or using too much memory.
Since Realm Database queries are lazy, there is no need to take such measures. Realm Database only loads objects from query results when they are explicitly accessed.
Collection notifications report objects as deleted when they drop out of the result set. This does not necessarily mean that they have been deleted from the underlying realm, just that they are no longer part of the query result.
You can reduce query results to unique values for a given field or fields using the distinct() method:
You can only call
distinct() on integer, long, short, and
fields; other field types will throw an exception. As with sorting,
you can specify multiple fields to resolve ties.
You can apply additional filters to a results collection by calling the where() method:
where() method returns a
RealmQuery that you can resolve into
RealmResults using a
find method. Filtered results can only
return objects of the same type as the original results set, but are
otherwise able to use any filters.
New in version 10.4.0.
You can use RealmQuery.rawPredicate() to query realms using Realm Query Language, a string-based query language to constrain searches when retrieving objects from a realm. For more information about syntax, usage and limitations, refer to the Realm Query Language reference.
Realm Query Language can use either the class and property names defined
in your Realm Model classes or the internal names defined with
You can combine raw predicates with other raw predicates or type-safe
predicates created with