Fetch data from Room Asynchronously with Coroutine(Flow) and Update UI With LiveData, Observe data change in Database In Android (Kotlin)

Pratibha Burde
2 min readMay 3, 2021

--

We can insert, update data into room database asynchronously with coroutine by calling suspend insert method we write inside DAO with below block

coroutineScope.launch{ // call suspended insert method here}

But we can not get data asynchronously with this approach. Fetching data is also long running operation but as we want the result back from it on UI thread and we want to observe database change for that Android hasn't provided any callback to coroutine.launch{} block

Our objective is, We want to fetch data from room database and always observe the result for any change database while lifecycleOwner is alive. (lifecycleOwner is the activity/fragment where we are observe database change). And process of fetching data should be on IO thread that keeps Main thread free to do UI work. and pass result back on main UI thread so that we can use result to update UI like notify recyclerview.

So , This can be possible with Flow combine with LiveData in Room Database when you are not using RxJava.

In DAO

@Query("SELECT * FROM grocery")
fun getAll(): Flow<List<Grocery>>

In Repository class

lateinit var groceries: Flow<List<Grocery>>init {
val db = GroceryManagementDb.getDatabase(application)
groceryDao = db!!.groceryDao()
groceries = groceryDao.getAll()
}

In ViewModel

we now convert Flow to LiveData

val groceries: LiveData<List<Grocery>> = groceryRepository.groceries.asLiveData()

In Fragment

model.groceries.observe(viewLifecycleOwner, Observer { groceries ->
Log.i("TAG", " Coroutine groceries size ${groceries.size}")
// You can notify adapter here
}
)

This observe block receives callback for data change in database like if any insert operation is happens while you are watching list of data. And we can do other operations like insert, update, delete with suspend , coroutine.launch{}

below is the reference link

https://github.com/googlecodelabs/android-room-with-a-view/tree/kotlin

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Pratibha Burde
Pratibha Burde

Written by Pratibha Burde

0 Followers

Android developer

No responses yet

Write a response