Use runBlocking to Execute Coroutine/Suspend Function (Kotlin Coroutines) in Worker/Backgroung Thread

If you are already in a worker/backgroud thread (e.g. WorkManager), use runBlocking to execute coroutine/suspend function.

class ProcessImageWorker constructor(context : Context, params : WorkerParameters)    : Worker(context, params) {    @WorkerThread    override fun doWork(): Result {        return runBlocking {            // execute suspend function, network call perhaps            val data = service.await()            // convert callback to coroutine            val result = suspendCoroutine<Int> { continuation ->                liveData.observeForever(object : Observer<String> {                    override fun onChanged(message: String?) {                        message?.let {                            liveData.removeObserver(this)                            continuation.resume(if (message == "ok") 1 else 0)                        }                    }                })            }            return if (result == 1) Result.success() else Result.failure()        }    }}

runBlocking Runs new coroutine and blocks current thread interruptibly until its completion. This function should not be used from coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.