haroldadmin
11/19/2018, 4:32 PMarthur.n
11/19/2018, 4:34 PMarthur.n
11/19/2018, 4:37 PMMike
11/19/2018, 4:57 PMharoldadmin
11/19/2018, 5:30 PMharoldadmin
11/19/2018, 5:31 PMarthur.n
11/19/2018, 5:37 PMharoldadmin
11/19/2018, 5:46 PMJoris PZ
11/20/2018, 8:01 AMDispatcher.Main
is a dispatcher that runs all coroutines on the Android UI thread, which is a single thread. But the default dispatcher Dispatcher.Default
uses at least two threads, and it is very possible that your coroutine will continue on another thread than on which it initially ran.haroldadmin
11/20/2018, 10:03 AMJoris PZ
11/20/2018, 10:20 AMsuspend myFunction() {
val x = 1
val y = otherSuspendingFunction(x)
println(x + y)
}
At the point where otherSuspendingFunction
is called, myFunction
gets suspended until otherSuspendingFunction
returns. The code below the suspending function call is effectively turned into a callback by the compiler, i.e.
otherSuspendingFunction(x) { result -> println(x + result)}
Coroutines are in that sense very much like async/await
, in that it allows you to write asynchronous code without the syntactic overhead and noise of callbacks or promises.
I don't really know if otherSuspendingFunction
will usually be called on the calling thread, or if it will be scheduled completely independently on any of the threads available to the dispatcher.
2. Coroutines get suspended anytime you call a suspending function. In IntelliJ IDEA you can see these suspension points marked in the gutter with this symbol: ⏸️
Again, I am trying to figure this stuff out myself, so I'll be happily corrected if I got anything wrong hereharoldadmin
11/20/2018, 10:29 AMsuspend
?
Suppose I have a computationally intensive function that doesn't call any other suspending functions. If I call it inside a coroutine, it blocks until the execution is complete.
Now if I mark this function with suspend
, I expect that the coroutine will pause when it reaches this function, and resume when this function returns. However, when I try to do this (mark this function with suspend), I get an IDE warning:
"Redundant 'suspend' modifier: suspend modifier is redundant if no other suspend functions are called inside."EDIT: I now realize that what I'm expecting by marking the function with
suspend
is wrong.haroldadmin
11/20/2018, 10:32 AMharoldadmin
11/20/2018, 10:38 AMJoris PZ
11/20/2018, 10:52 AMharoldadmin
11/20/2018, 11:10 AMsuspend fun getStockPrice(ticker: String): String {
return java.net.URL(
"<http://localhost:8085?ticker=$ticker>").readText()
}
It is a great talk otherwise, though. Thanks for sharing this!gildor
11/21/2018, 4:49 AMgildor
11/21/2018, 4:50 AMgildor
11/21/2018, 4:52 AMwithContext(SOME_DISPATCHER)
so it would be safe to call this function without blocking your current thread, but some thread from dispatcher (SOME_DISPATCHER
) will be blocked.
Also problem of this that such operation will not be cancellable, and will be cancelled only when blocking code is finished and function returned.
So just for this case (http request) better to use any async API + coroutine dispatcherharoldadmin
11/21/2018, 6:52 AMharoldadmin
11/21/2018, 6:52 AMgildor
11/21/2018, 6:55 AM