asFlow

fun <T : BaseRealmObject> T.asFlow(keyPaths: List<String>? = null): Flow<ObjectChange<T>>

Observe changes to a Realm object. The flow would emit an InitialObject once subscribed and then, on every change to the object an UpdatedObject. If the observed object is deleted from the Realm, the flow would emit a DeletedObject and then will complete, otherwise it will continue running until canceled.

The change calculations will be executed on the thread represented by Configuration.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 object.

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 syntax, e.g. parent.child.name. Wildcards * can be be used to capture all properties at a given level, e.g. child.* or *.*. If no keypaths are provided, changes to all top-level properties and nested properties 4 levels down will trigger a change.

Throws

if called on a live RealmObject or EmbeddedRealmObject from a write transaction (Realm.write) or on a DynamicRealmObject inside a migration (AutomaticSchemaMigration.migrate).

if an invalid keypath is provided.