collect

inline fun <T : Any> collect(noinline onResult: (result: T?, error: Throwable?) -> Unit, collectionCount: Int? = null, dispatcher: CoroutineDispatcher = Dispatchers.Default): DataCollector<T>

Creates and starts an DataCollector for continuous or a specific number of collections.

This factory function provides a clean, type-safe API for creating a collector. Thanks to inline and reified, you can specify the target data class T as a generic parameter (e.g., collect<EventsData>(...)) without needing to pass the class reference (EventsData::class) manually. This is the recommended way to create an instance.

Return

A new, active instance of DataCollector.

Parameters

T

The data class type to be collected and instantiated. It must be reified.

onResult

The callback lambda that will be invoked for each successful collection or error.

collectionCount

The number of times to collect. Defaults to null for continuous collection.

dispatcher

The CoroutineDispatcher for the internal scope. Defaults to Dispatchers.Default.

Throws

if the provided generic type T is not a data class.