Room Database Getting Started - Lesson 22 02:14 Incorrect explanation about coroutines

The author explains below code:

lifecycleScope.launch {

bookDao.getBooks()

}

override suspend fun getBooks(): List = bookDao.getBooks()

Coroutine will starts in main thread and switch to “Background” thread while doing database work and then switch back to “Main” thread.

But this is incorrect. Because there is not thread switching in the above code and the database operation will still run in “Main” thread: The shuld change like:

override suspend fun getBooks(): List = withContext(Dispatchers.IO) {
bookDao.getBooks()
}

This topic was automatically closed after 166 days. New replies are no longer allowed.