The current state of Coroutines is not well suited for Kotlin native. See issue 462
As a workaround, Trikot provides Threads (Android Worker, iOS Operation Queue) for running background operations. They are called DispatchQueues.
Once KMP memory model will be addressed, default dispatch queues provided by Trikot will be migrated to Coroutines. See Using Coroutines section below for our migration strategy.
val dispatchQueue = OperationDispatchQueue()
dispatchQueue.dispatch {
// Heavy cpu operation
}
- Dispatch queue for background operations. Number of threads depends on the platform implementation and the number of processors
- Special queue that ensure than operation will be executed sequentially. Will be executed synchronously if no operation are currently running.
- Dispatch operation synchronously. Usefull for testing.
By default, trikot provide those dispatch queues available trough the Configuration object
- FoundationConfiguration.publisherExecutionDispatchQueue -> Default queue used by
Trikot.streams
to execute code - FoundationConfiguration.serialSubscriptionDispatchQueue -> Queue that ensure Serial execution along your systems.
Like Coroutines, one class can implement QueueDispatcher
and dispatch event using
class Foo(val dispatchQueue: DispatchQueue
): QueueDispatcher {
fun bar() {
dispatch {
// Stuff to run on other thread
}
}
}
The following code runs ExecutablePublisher on coroutines instead of threads
class MyCoroutineDispatcher(val context: CoroutineContext = Dispatchers.unconfined): DispatchQueue, CoroutineScope {
fun dispatch(block: DispatchBlock) {
launch {
block()
}
}
}
FoundationConfiguration.publisherExecutionDispatchQueue = MyCoroutineDispatcher()