Created
September 4, 2018 22:03
-
-
Save binaryroot/18eb265b6bc73c89f02279e39171ed63 to your computer and use it in GitHub Desktop.
How coroutines switch back to the main thread.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
launch(context = UI) { | |
val result = async { loadString() }.await() | |
Log.d("TAG", | |
"Post execution thread:"+Thread.currentThread().name) | |
textView.text = result | |
} | |
} | |
private suspend fun loadString() : String { | |
Log.d("TAG", "Execution thread: "+Thread.currentThread().name) | |
delay(1000L) | |
return "Some loaded string..." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which version of coroutines did you use to have the launch method in the Activity?