The simple way to convert to use Livedata with room database and experiment database inspector live update
Recently Google has introduced the database inspector with the example sunflower. You may also want to try with the live update of the database inspector and it can be a tricky task and burn a bit time to convert
room database without livedata to room database with livedata
and experiment with the live update of the database inspector. In this post, I will use the existing project using in the experimenthilt
from Google that has some modifications in my repo.
This post will focus on convert to use the livedata
with room and experiment.
The source code before applying this change
Convert to using the livedata
Step1: Add lifecycle-livedata
dependency on the app/build.gradle.kts
// live data
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
then run Gradle sync by click to the Gradle icon on your Android studio.
Step2: Update the com.example.android.hilt.data.LogDao
interface.
Original LogDa
the interface doesn’t use the live data to get the logs.
@Dao
interface LogDao {
@Query("SELECT * FROM logs ORDER BY id DESC")
fun getAll(): List<Log>
...
}
Convert this method to use livedata
for getting logs
@Dao
interface LogDao {
@Query("SELECT * FROM logs ORDER BY id DESC")
fun getAll(): LiveData<List<Log>>
...
}
Study more if you don’t clear how to use the livedata
with room
.
Step3: Update the LoggerDataSource
interface to use the livedata
Original
interface LoggerDataSource {
fun addLog(msg: String)
fun getAllLogs(callback: (List<Log>) -> Unit)
fun removeLogs()
}
The new update
interface LoggerDataSource {
val liveData: LiveData<List<Log>>
fun addLog(msg: String)
fun removeLogs()
}
Step4: Update the implementation of LoggerDataSource
LoggerLocalDataSource
will become
@Singleton
class LoggerLocalDataSource @Inject constructor(private val logDao: LogDao) : LoggerDataSource {
override val liveData: LiveData<List<Log>> = logDao.getAll()
private val executorService: ExecutorService = Executors.newFixedThreadPool(4)
override fun addLog(msg: String) {
executorService.execute {
logDao.insertAll(
Log(
msg,
System.currentTimeMillis()
)
)
}
}
override fun removeLogs() {
executorService.execute {
logDao.nukeTable()
}
}
}
We remove the unused property and assign directly the livedata
value from logData
We also need to update the LoggerInMemoryDataSource
to avoid compile errors.
Step5: Inject the livedata
to the LogsFragment
by inserting the snipped code below to the onViewCreated
logger.liveData.observe(viewLifecycleOwner, Observer {
binding.recyclerView.adapter =
LogsViewAdapter(
it,
dateFormatter
)
})
Here the livedata
is observed with the viewLifecycleOwner
then update the new value to the UI when the database update that makes the UI automatically update when the data source change without refresh.
Congratulation!!!
We have converted from room to use the livedata
.
Experiment live update of the database inspector
This feature only works with a database that is using the livedata
. To use the database inspector, we need to open the project with the Android studio 4.1 or above.
Step1: Build and run the project on an emulator or device.
Step2: Click some buttons to generate some logs
Step3. Navigate to the logs fragment by pressing SEE ALL LOGS
We will have the screen with some logs above.
Step4: Open the database inspector by choosing one of two ways below
- Click View -> Tool Windows -> Database Inspector
- Click to the Database INspector in the bottom left of your Android studio
Step5: Choose device and application
Step6: Open the logs table and change the value of one row and observe the change on the application UI.
Congratulation, you have known how to use the database inspector and live update.
The source code after converting to room database with livedata