Suspend Function In Kotlin-Coroutines

Suspend Function In Kotlin-Coroutines

Simple Function To Execute LongRunning Operations || Tasks Without Blocking, Which Can Be Paused && Resumed Later.

·

2 min read

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

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

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 Have 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

->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 To Mention suspend Before You Create The Function:

suspend fun delaying() {
        //Code
}

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

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

->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😷"
    }

Note: You Should NOT Use GlobalScope every time.

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

GlobalScope.launch {
  delaying()
}

->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😷"
}

->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🤗