27
loading...
This website collects cookies to deliver better user experience
Make Sure
That You Have Included Coroutines Dependency If It's Not Included:implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
delay()
Is A Suspend Function That delays a coroutine for a specific time. But It Won't Block
Whole Thread.runBlocking
In Coroutines Which Will Start A New Coroutine When You Assign It, Which Starts In Main Thread By Default.runBlocking
Will Block Whole Thread But GlobalScope.launch(...){ }
Won't.Confusing🤔? Not A Problem🙌,
1.
If If Implement delay()
In GlobalScope.launch(...){...}
, I Can Still Able To Operate UI From My Coroutine Even runBlocking()
Is Executed:2.
In This Example I'll Implement delay()
In runBlocking()
, So That You'll Get An Idea About runBlocking()
And What's The Actual Use In Real.I Hope Your Confusing Regarding runBlocking()
Has Cleared.
runBlocking()
, But Why Would Someone Block UI Updates Or Other Stuff In The Application🤔?runBlocking()
Is That You Can Actually Play Around With Coroutines And runBlocking() To Check What's Actually Going On In Coroutines. In Other Words To Check Behind-The-Scenes Of Coroutines.runBlocking()
Will Be Synced With Our Normal MainThread Flow.runBlocking
Will Start A New Coroutine When You Assign It*, That Mean We Can Launch Multiple Coroutines From The runBlocking
Too.GlobalScope.launch(...){...}
And App Will Notify Those Changes In Both Logcat And In Application:GlobalScope.launch(Dispatchers.Main) {
Log.d("runBlocking", "Started Execution From GlobalScope")
delay(2000L)
textView.text = "In Global Scope"
}
runBlocking {
launch(Dispatchers.IO) {
Log.d("runBlocking", "Started Execution From runBlocking")
}
launch(Dispatchers.IO) {
Log.d("runBlocking", "Started Execution From Another runBlocking")
}
}
Bye🤗