Coroutines

Suspending functions in Coroutine

Pinterest LinkedIn Tumblr

In this blog, we will learn suspending functions in a coroutine. I will show you how it works in coroutine android. What needs of these in a coroutine, etc. How they make callbacks seamless. So let’s started.

What is suspending functions

The idea behind suspending functions is quite easy. It simply a function that can be run in a coroutine. In simple words, a functions that can be run in a coroutine, and the main advantage I would say of suspending functions is that they make callbacks seamless.

Need of suspending functions

Actually In coroutine, If you try to access a function from coroutine you’ll get an error unless you make it a suspending function

Suspending functions example

let take a very basic example of suspending functions and understand how it makes developer life easier. So let started

In this example, I’m going to use the function named composeMessage() and one variable named count that equal to zero. So using this variable we simply track the number of calls to suspend the function. Let see the improvements things is the count (variables) is part of our main application. However, suspending functions composeMessage() will be part of Coroutine. Here is how easy it is to synchronize between our main thread and a Coroutine.

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

var count = 0

fun main() {
    
 }

suspend fun composeMessage() {
    delay(500L)
    println("Android!")
    count++
}

Create improveMessage()

Let’s go ahead and create another function let say improveMessage(). This function also does kind of the same things. So here is delay is 1 second and simply println a message somethings like below.

suspend fun improveMessage() {
    delay(1000L)
    println("Suspend functions are cool")
    count++
}

Call functions in main thread

Now in our main program, let go head and launch these two coroutine just like below.

  import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

var count = 0

fun main() {
    GlobalScope.launch { composeMessage() }
    GlobalScope.launch { improveMessage() }
    print("Hi, ")
    Thread.sleep(2000L)
    println("There have been $count calls so far")
}

suspend fun composeMessage() {
    delay(500L)
    println("Android!")
    count++
}

suspend fun improveMessage() {
    delay(1000L)
    println("Suspend functions are cool")
    count++
}

Output

Now let run the programme and the expected out is below.

Hi, Android!
Suspend functions are cool
There have been 2 calls so far

So in console, we are getting Hi, half a second later. We are getting the Android! . Then we are getting a second message from improveMessage() function. And then we are checking the variable count. An important thing we are accessing that variable in the main thread that has been updated by the Coroutine. So this is how synchronization works. It is very easy to update functions or variables in the main thread from the Coroutine.

Conclusion

It’s not a very complicated fictionality and it’s probably not very useful in a real-world situation. But I’m sure it does highlight how we can run a function from a Coroutine. also importantly how we can update local variables from a Coroutine.

What is recommendation

If you are a beginner I would like to recommend please read basic concept of coroutines.

I hope this article helps you a lot, I would like to suggest you read our coroutines series. 

Write A Comment