Reputation: 62519
i was wondering since both withContext and suspendCoroutine are suspend functions is there any difference betweeen them other then the fact that suspendCoroutine offers a continuation so you can control when it resumes or cancels if we use suspendCancellableCoroutine variation. I would say both can be used to stop making callbacks (which is one advantage of coroutines). is there any major difference ?
Upvotes: 3
Views: 2795
Reputation: 200168
Actually only suspendCoroutine
can be used to translate a callback-based API into coroutines. withContext
doesn't have the effect of suspending a coroutine, but temporarily changing its context (this is mainly about changing the dispatcher). The coroutine immediately continues in the other context and then comes back to the caller's context.
Upvotes: 10