问题
fun startAsyncFunc() {
launch {
asyncFunc1()
asyncFunc2()
}
}
fun asyncFunc1() { ... }
suspend fun asyncFunc2() { ... }
I can finish the work without suspend
and it even makes test easier (it can be tested without adding runBlocking
.
My questions:
asyncFunc1
vsasyncFunc2
, which is better and why?- If
asyncFunc2
is better, should I always usesuspend
whenever a function will be ran in the coroutines?
Update
In the recent releases of Kotlin Coroutines, I notice if a method doesn't contain any coroutines code(like launch
, async
, etc), the compiler complains This inspection reports a suspend modifier as redundant if no other suspend functions are called inside
. So I assume that suspend
should be only used when it's a must.
回答1:
You should only declare your function suspend
if it needs to. I would say that, when in doubt, if the compiler does not force you, don't use suspend
.
Most of the time, if you have a good reason for your function to be suspending, it means it's doing something that probably requires you to use suspending functions like withContext
anyway, and this will force you to declare your function suspend
.
Note that declaring a function suspend
does not enable your callers to do anything more than they could when your function was not suspending. If anything, you're limiting the use of your function.
I believe one use case for a function to be suspending without being forced to is when you really absolutely positively want to show the world that your function is computationally heavy, and thus force your callers to deal with the suspension.
回答2:
suspend
keyword means coroutine can be suspended for later execution.
Having said that, you should consciously use it them for coroutines that will get suspended (e.q. your asyncFunc2()
made a HTTP call and is awaiting response to process it)
So.
- Use
suspend
for functions that will be delayed in some way (Awaiting some computations, api response etc.) suspend fun
can be run from coroutine only. So, if it gets suspended, it will block the coroutine. Take out thesuspend
keyword, but run it in coroutine and it will have the same effect. However, if you run this function from outside the coroutine, it will block the thread it was running on.
When testing coroutines, you should always invoke runBlocking
. If you don't, a coroutine that gets suspended may not complete, resulting in a failed test.
回答3:
- It depends. See the combination of the other two answers at this time: the effect of calling either function from a coroutine is identical. However, with the
suspend
keyword, the function itself can call other suspending functions. Using the keyword can be an indication of some work that requires time and therefore might need to suspend the calling coroutine. - A function that will always be called from a coroutine does not need to always have the
suspend
keyword. It only needs that keyword for the reasons given under 1. The other way around is true: a suspending function can only be called from a coroutine.
回答4:
From the docs:
Suspending functions can be used inside coroutines just like regular functions, but their additional feature is that they can, in turn, use other suspending functions, like delay in this example, to suspend execution of a coroutine.
来源:https://stackoverflow.com/questions/54554753/when-to-use-kotlin-suspend-keyword