https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

adeln

02/06/2019, 11:02 AM
Hey folks, I have a question https://stackoverflow.com/q/54552056/1032286 would really appreciate if someone can help me with this
nevermind, answered it myself
the resulting coroutine can be used in
.produce{}
to create a channel
❤️ 1
a

adeln

02/06/2019, 11:56 AM
huh. Creating a coroutine per each listener trigger?
Copy code
suspend fun View.globalLayout(): View =
    suspendCancellableCoroutine { cont ->
        val listener = ViewTreeObserver.OnGlobalLayoutListener {
            cont.resume(this)
        }

        cont.invokeOnCancellation {
            viewTreeObserver.removeOnGlobalLayoutListener(listener)
        }

        viewTreeObserver.addOnGlobalLayoutListener(listener)
    }

fun View.globalLayouts(context: CoroutineContext = Dispatchers.Unconfined): ReceiveChannel<View> =
    GlobalScope.produce(context) {
        while (true) {
            send(globalLayout())
        }
    }
?
*creating a new continuation per listener trigger
2 Views