asFlow

abstract fun asFlow(keyPaths: List<String>? = null): Flow<SingleQueryChange<T>>

Observes changes to the first object that fulfills the query conditions. The flow will emit SingleQueryChange events on any changes to the first object represented by the query. The flow will continue running indefinitely until cancelled.

If subscribed on an empty query the flow will emit a PendingObject event to signal the query is empty, it would then yield an InitialObject event for the first element. On a non-empty list it would start emitting an InitialObject event for its first element.

Once subscribed and the InitialObject event is observed, sequential UpdatedObject instances would be observed if the first element is modified. If the element is deleted a DeletedObject would be yield.

If the first element is replaced with a new value, an InitialObject would be yield for the new head, and would be follow with UpdatedObject on all its changes.

                  ┌───────┐
┌─────┤ Start ├───┐
│ └───────┘ ├────┐──────────┬─────┐
┌───────▼───────┐ ┌───────▼────┴──┐ ┌─────┴─────▼───┐
│ PendingObject ├─► InitialObject │ │ UpdatedObject │
└───────────────┘ └───────▲───────┘ └───────────┬───┘
│ ┌───────────────┐ │
└──► DeletedObject ◄──┘
└───────────────┘

The change calculations will run on the thread represented by RealmConfiguration.Builder.notificationDispatcher.

The flow has an internal buffer of Channel.BUFFERED but if the consumer fails to consume the elements in a timely manner the coroutine scope will be cancelled with a CancellationException.

Return

a flow representing changes to the RealmObject or EmbeddedRealmObject resulting from running this query.

Parameters

keyPaths

An optional list of properties that defines when a change to the object will result in a change being emitted. Nested properties can be defined using a dotted syntex, e.g. parent.child.name. If no keypaths are provided, changes to all top-level properties and nested properties 4 levels down will trigger a change.

Throws

if an invalid keypath is provided.