DEV Community

Cover image for Suspend Function In Kotlin-Coroutines
Saketh
Saketh

Posted on

Suspend Function In Kotlin-Coroutines

Before Getting Started I Suggest You Understand What's Actually A Coroutine Is, Which I Have Discussed In The Previous Blog. And Make Sure That You Have Included Coroutines Dependency If It's Not Included:



implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'


Enter fullscreen mode Exit fullscreen mode

Suspend Function: Well, Suspend Function Is A Simple Function Which Can Be Paused And Resumed Later In Your Code.

Suspend Function Can Execute Long-Running Operations || Tasks And Will Wait For It To Complete Without Blocking The Whole Code.

And The Speciality Of Suspend Function Is That It Can Be Only Implemented In Another Suspend Function || Coroutine.

delay() Is Also A Suspend Function That delays the coroutine for a specific time.

If You Are Trying To Implement delay() In Your Code, Your IDE May Show This Image In The Numerical Panel Where You Implemented A Suspend Function👇🏼

Suspend Function Symbol.png

This Image Simply Means That You Are Implementing A Suspend Function.

As I Mentioned That delay() Is Also A Suspend Sunction Which Means That You Should Implement delay() In A Suspend Function Or In A Coroutine

->But In Case If You Are Trying To Implement Suspend Function Outside Of A Supend Function Or A Coroutine Leads To This Beautiful Error🤭:



Suspend function 'delay' should be called only from a coroutine or another suspend function


Enter fullscreen mode Exit fullscreen mode

->Error May Look Like This If You Are Trying To Implement Suspend Function Outside Of A Supend Function Or A Coroutine In Your Android Studio:

DelayFunctionError.png
->Let's Build A Suspend Function In Android To Deep-dive:

->For Creating A Suspend Function We Need Mention suspend Before You Create The Function:



suspend fun delaying() {
        //Code
}


Enter fullscreen mode Exit fullscreen mode

->Returning A String Value From The delaying() Function:



suspend fun delaying():String{
        return "Stay Safe😷"
}


Enter fullscreen mode Exit fullscreen mode

->As I Mentioned That Suspend Function Can Be Called || Implemented In Suspend Function Or In A Coroutine.

->Let's Launch A Coroutine By Assigning GlobalScope.launch{} In Your Main Thread:



override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GlobalScope.launch {
            //Code
        }
    }
    suspend fun delaying():String{
        return "Stay Safe😷"
    }


Enter fullscreen mode Exit fullscreen mode

Note: You Should NOT Use GlobalScope every time.

->Now Let's Implement delaying() In Coroutine:



GlobalScope.launch {
  delaying()
}


Enter fullscreen mode Exit fullscreen mode

->Now If You Are Trying To Implement delaying() Outside, Of A Coroutine You'll Get This Error As delaying() Is A Suspend Function:

suspend delaying() error.png
->Let's Assign LogTo Check If The delaying() Is Working Or Not:



GlobalScope.launch {
            val checking=delaying()
            Log.d("suspend", "${checking} Is Running In ${Thread.currentThread().name}")
}

suspend fun delaying():String{
   delay(2000L)
   return "Stay Safe😷"
}


Enter fullscreen mode Exit fullscreen mode

->Now Run The Application And Open Logcat To Check The Result:

logcat message.png
Well That's All For Now, In Upcoming Blog I'll Discuss Coroutine Contexts😉

Bye🤗

Top comments (0)