RealmQuery

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

Link copied to clipboard
abstract fun asFlow(keyPath: List<String>? = null): Flow<ResultsChange<T>>

Finds all objects that fulfill the query conditions and returns them asynchronously as a Flow.

Link copied to clipboard
abstract fun count(): RealmScalarQuery<Long>

Returns a RealmScalarQuery that counts the number of objects that fulfill the query conditions.

Link copied to clipboard
abstract fun description(): String

Returns a textual description of the query.

Link copied to clipboard
abstract fun distinct(property: String, vararg extraProperties: String): RealmQuery<T>

Selects a distinct set of objects of a specific class. When multiple distinct fields are given, all unique combinations of values in the fields will be returned. In case of multiple matches, it is undefined which object is returned. Unless the result is sorted, then the first object will be returned.

Link copied to clipboard
abstract fun find(): RealmResults<T>

Finds all objects that fulfill the query conditions and returns them in a blocking fashion.

Link copied to clipboard
fun <T : BaseRealmObject, 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.

Link copied to clipboard
abstract fun first(): RealmSingleQuery<T>

Returns a query that finds the first object that fulfills the query conditions.

Link copied to clipboard
abstract fun limit(limit: Int): RealmQuery<T>

Limits the number of objects returned in case the query matched more objects.

Link copied to clipboard
abstract fun <T : Any> max(property: String, type: KClass<T>): RealmScalarNullableQuery<T>

Finds the maximum value of a property.

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.

Link copied to clipboard
abstract fun <T : Any> min(property: String, type: KClass<T>): RealmScalarNullableQuery<T>

Finds the minimum value of a property.

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.

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.

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.

Link copied to clipboard
abstract fun <T : Any> sum(property: String, type: KClass<T>): RealmScalarQuery<T>

Calculates the sum of the given property.

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.