SuspendCoroutine
suspendCoroutine

SuspendCoroutine

Today I will be writing about the usage of an important concept in Android namely suspendCoroutine.

By definition suspendCoroutine helps us bridge the gap between callback based results(asynchronous) and coroutine block code.

Let us visualise a coroutine block executing a flow and the flow should only proceed ahead of a certain point only after that certain point completes its execution/purpose. And that certain point is where we are doing an api call which is as we know unpredictable in terms of its completion time - this is our use case.

In this case we tend to use something known as suspendCoroutine.

Now check the below code -

fun main() {
    CoroutineScope(Dispatchers.IO).launch {
        // some code
        val result = suspendCoroutine<Pair<String, String>> { continuation ->
           // api call
            viewModel.uploadHomeWork(imageFileName) { url1, url2 ->
                if (url1.isEmpty() || url2.isEmpty()) {
                    continuation.resume(Pair("", ""))
                } else {
                    viewModel.uploadImage(url2, imageFile)
                    continuation.resume(Pair(url1, url2))
                }
            }
        }
        // some more code 
        // needs the url1 and url2 to proceed
    }
}        

In this case, we want to run some code before the api call and some more code after the api call. But the {// some more code} part needs data from the above api call to complete itself.

Inside the suspendCoroutine block, we receive a continuation object. This continuation acts as a state machine, meticulously tracking the status and state of the asynchronous operation. It holds the coroutine in a suspended state until the callback is invoked. Once the operation completes, the callback uses the continuation to resume the coroutine.

Further in case of any error in the api call we can resume the flow using resumeWithException().

For more clarity check the below source code -

resumeWith and resumeWithException

I hope we learned something new today. Thanks for reading.

Do follow me for more such Android concepts.

要查看或添加评论,请登录

Siddhant Mishra的更多文章

  • findViewById - Internals

    findViewById - Internals

    Hope everyone is doing well. Today I will be writing about the internal working of findViewById in Android.

    2 条评论
  • Lambda functions

    Lambda functions

    Today I will be writing about two different yet intriguing way to write the lambda functions in Kotlin. Conventionally…

    1 条评论
  • Exceptions in Coroutines

    Exceptions in Coroutines

    Hi everyone, today I will be writing about exceptions in Coroutine and how to handle them gracefully. First let us see…

  • Trie data structure

    Trie data structure

    Today I will write about a magical data structure namely Trie. When we talk of storing and retriving some information…

  • Vertical Order traversal in binary tree

    Vertical Order traversal in binary tree

    We generally talk about Inorder, PreOrder, PostOrder and LevelOrder traversals in binary tree, but we generally do not…

  • LCA in Binary Tree

    LCA in Binary Tree

    Today I will be writing about the LCA(Least Common Ancestor) for a binary tree. LCA refers to the node in the binary…

  • Delegates in Kotlin

    Delegates in Kotlin

    Today I will be writing about my understanding regarding the delegates in Kotlin. The english meaning of Delegation…

  • TypeAlias in Kotlin

    TypeAlias in Kotlin

    Today I will be writing about TypeAlias in Kotlin and how do I understand and use it in my day to day coding. Typealias…

  • Reified in Kotlin

    Reified in Kotlin

    Today I will be writing about the type erasure in Java and Kotlin and its solution. The concept of type erasure stems…

    1 条评论
  • Jetpack Compose - Interesting features to dive deeper into.

    Jetpack Compose - Interesting features to dive deeper into.

    Recently I got to migrate my sample project from the conventional XML to Jetpack Compose, and I would like to list down…

    3 条评论

社区洞察

其他会员也浏览了