Suspend Function In Kotlin-Coroutines
Saketh
Posted on June 30, 2021
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'
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
👇🏼
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:
->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
}
->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:
->Let's Assign Log
To 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:
Well That's All For Now, In Upcoming Blog I'll Discuss Coroutine Contexts😉
Bye🤗
Posted on June 30, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.