There is a very common pattern using CountDownLatch
when developing with Android, sometimes you want to make an async implementation to be sync when dealing with BroadcastReceivers
and CountDownLatch
is pretty handy. The AOSP is filled with CountDownLatch(1)
.
private suspend fun yourSuspendMethod() {
val job = GlobalScope.async {
val latch = CountDownLatch(1)
val watcher = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {