Wilson Castiblanco
05/20/2019, 5:50 PMstreetsofboston
05/20/2019, 5:59 PMWilson Castiblanco
05/20/2019, 6:12 PMfun getDescription(code: String): String = runBlocking {
val tour = localRepository.getTourByCode(code) // This is a Coroutine getting the Object from Room
return@runBlocking tour.description
}
This is the snippet, is pretty simple.
Yeah, I don't want to block the Main Thread, but, the process seems to be synchronous.streetsofboston
05/20/2019, 6:13 PMrunBlocking
. Don’t use this.
Let me whip up a quick example based on your snippet…Wilson Castiblanco
05/20/2019, 6:13 PMrunBlocking
that will prevent to block the Main Thread? 🤔getDescription(..)
should be synchronous, correct me if I'm wrong.streetsofboston
05/20/2019, 6:16 PMclass MyActivity: AppCompatActivity(), CoroutineScope by MainScope() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
launch {
textView.text = getDescription("Desc")
}
}
suspend fun getDescription(code: String): String {
val tour = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { localRepository.getTourByCode(code) }
return tour.description
}
override fun onDestroy() {
super.onDestroy()
cancel()
}
}