RealmQuery

interface RealmQuery<T : RealmObject> : RealmElementQuery<T>

A RealmQuery encapsulates a query on a Realm, a RealmResults or a RealmList instance using the Builder pattern. The query is executed using either find or subscribing to the Flow returned by asFlow.

A Realm is unordered, which means that there is no guarantee that a query will return the objects in the order they where inserted. Use the sort functions if a specific order is required.

Results are obtained quickly most of the times when using find. However, launching heavy queries from the UI thread may result in a drop of frames or even ANRs. If you want to prevent these behaviors, you can use asFlow and collect the results asynchronously.

Parameters

T

the class of the objects to be queried.

Functions

asFlow
Link copied to clipboard
abstract override fun asFlow(): Flow<RealmResults<T>>
Finds all objects that fulfill the query conditions and returns them asynchronously as a Flow.
count
Link copied to clipboard
abstract fun count(): RealmScalarQuery<Long>
Returns a RealmScalarQuery that counts the number of objects that fulfill the query conditions.
distinct
Link copied to clipboard
abstract fun distinct(property: String, vararg extraProperties: String): RealmQuery<T>
Selects a distinct set of objects of a specific class.
find
Link copied to clipboard
abstract fun find(): RealmResults<T>
Finds all objects that fulfill the query conditions and returns them in a blocking fashion.
first
Link copied to clipboard
abstract fun first(): RealmSingleQuery<T>
Returns a query that finds the first object that fulfills the query conditions.
limit
Link copied to clipboard
abstract fun limit(limit: Int): RealmQuery<T>
Limits the number of objects returned in case the query matched more objects.
max
Link copied to clipboard
abstract fun <T : Any> max(property: String, type: KClass<T>): RealmScalarNullableQuery<T>
Finds the maximum value of a property.
min
Link copied to clipboard
abstract fun <T : Any> min(property: String, type: KClass<T>): RealmScalarNullableQuery<T>
Finds the minimum value of a property.
query
Link copied to clipboard
abstract fun query(filter: String, vararg arguments: Any?): RealmQuery<T>
Appends the query represented by filter to an existing query using a logical AND.
sort
Link copied to clipboard
abstract fun sort(propertyAndSortOrder: Pair<String, Sort>, vararg additionalPropertiesAndOrders: Pair<String, Sort>): RealmQuery<T>
Sorts the query result by the specific property name according to Pairs of properties and sorting order.
abstract fun sort(property: String, sortOrder: Sort = Sort.ASCENDING): RealmQuery<T>
Sorts the query result by the specific property name according to sortOrder, which is Sort.ASCENDING by default.
sum
Link copied to clipboard
abstract fun <T : Any> sum(property: String, type: KClass<T>): RealmScalarQuery<T>
Calculates the sum of the given property.

Extensions

find
Link copied to clipboard
fun <T : RealmObject, R> RealmQuery<T>.find(block: (RealmResults<T>) -> R): R
Similar to RealmQuery.find but it receives a block in which the RealmResults from the query are provided.
max
Link copied to clipboard
inline fun <T : Any> RealmQuery<*>.max(property: String): RealmScalarNullableQuery<T>
Similar to RealmQuery.max but the type parameter is automatically inferred.
min
Link copied to clipboard
inline fun <T : Any> RealmQuery<*>.min(property: String): RealmScalarNullableQuery<T>
Similar to RealmQuery.min but the type parameter is automatically inferred.
sum
Link copied to clipboard
inline fun <T : Any> RealmQuery<*>.sum(property: String): RealmScalarQuery<T>
Similar to RealmQuery.sum but the type parameter is automatically inferred.