https://kotlinlang.org logo
Title
a

asad.awadia

01/02/2018, 1:11 PM
Hmm - i m still unsure of how coroutines will be used in real life situations.. other than syntactic sugar to avoid call back hell - is there anything it provides?
l

louiscad

01/02/2018, 1:16 PM
Can be used for async I/O, can be used to await events like the code was blocking a thread, but without blocking a thread. I see plenty of usages where doing so without coroutines would require a callback hell if even possible. I personally use coroutines for Bluetooth Low Energy connections: https://github.com/Beepiz/BleGattCoroutines
a

asad.awadia

01/02/2018, 1:18 PM
can be used to await events like the code was blocking a thread, but without blocking a thread
<--- this is exactly what I thought they provided too - do u have any examples?
h

hiperbou

01/02/2018, 1:20 PM
I use coroutines to encapsulate and to have a clean and common way to use all the async functions of third party libraries, for example.. some lib uses promises -> coroutine, other uses callbacks -> coroutine, other signals -> coroutine.. keeps my code clean and readable..
l

louiscad

01/02/2018, 1:22 PM
@asad.awadia Let's say you want to await an internet connection before attempting your network call. You could write a function that would suspend until there's an internet connection. Then, you would call
awaitInternetConnection()
in a coroutine (a suspend block), and your code would go past the
awaitInternetConnection()
call only when the internet connection is there, that is, when the method resumes from suspension, and would do this, without blocking any thread or locking up precious ressources
@asad.awadia Such behavior can implemented in different ways. I made a similar thing that suspends until a Bluetooth Low Energy connection is performed, which relies on this
Gate
class: https://github.com/Beepiz/BleGattCoroutines/blob/bdb7b82b2b46f813633ae471122c19754aab939c/blegattcoroutines/src/main/java/com/beepiz/bluetooth/gattcoroutines/experimental/Gate.kt
a

asad.awadia

01/02/2018, 1:25 PM
is
awaitInternetConnection()
a suspend function or is it of the form
fun awaitInternatConnection() = launch {//code}
but if i call it in a
runBlocking
it will block the thread which voids the whole point correct?
l

louiscad

01/02/2018, 1:26 PM
It can only work if
awaitInternetConnection()
is a suspend function
a

asad.awadia

01/02/2018, 1:26 PM
but what if the code inside the awaitInternetConnection is blocking?
the suspend doesn't do anything right --- the code inside needs to be non blocking in the first place
l

louiscad

01/02/2018, 1:27 PM
runBlocking blocks the thread, making it available solely for the coroutine inside the block. It is useful on JVM (not Android) to avoid the main thread to terminate, and allows to use it in a coroutines context
What blocking code are you expecting in an
awaitInternetConnection()
function?
a

asad.awadia

01/02/2018, 1:29 PM
wouldn't waiting for a connection inherently be blocking?
l

louiscad

01/02/2018, 1:32 PM
What I call blocking code is code that does synchronous I/O or significant computations. CPU computations should be done in
CommonPool
most of the time. Blocking I/O should be done on a thread pool properly sized for the running environment (like you'd do in Java), but for prototyping, having a global (top-level)
val IO = CommonPool
is okay as long as it doesn't locks up CPU-bound computations that are normally the stuff
CommonPool
is made for.
You then use
CommonPool
,
IO
or other coroutine contexts as arguments for
async(...) { ... }
and
launch(...) { ... }
And no, waiting for an internet connection is not inherently blocking. In Android for example, you get a callback in a BroadcastReceiver when connectivity changes, where you can check again if the active connection (if any) is connected to internet. You could then feed this to a
BroadcastChannel
consumed by various parts of your program, or feed it to a class like the
Gate
class I showed you earlier, that could the be used to write a suspending
awaitInternetConnection()
suspend function