How to check if data is inserted in Room Database

后端 未结 7 2708
执念已碎
执念已碎 2021-02-19 22:07

I am using Room Database from the new Architecture components in my project. I am adding some data through dao, but when trying to retrieve it I am not getting it. Can you pleas

7条回答
  •  遥遥无期
    2021-02-19 22:19

    The problem was in the thread. Room doesn't allow you to run database queries in main thread. The call to insert method was inside of a try-catch block, and I was ignoring the exception. I fixed it, and here's how it reads right now.

    doAsync {
                            val addedID = appContext.db.rallyDAO().addVehicleListItem(vehicle)
                            Logger.d("vehicle_lsit_item","Inserted ID $addedID")
            }
    

    Also, I reviewed the documentation, the insert method may return a Long (or List of Long in case of List passed to insert). I also changed the signature of insert, and here is the modified code

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun addVehicleListItem(vehicleListItem:VehicleListItem):Long
    

    P.S. I am using anko for doAsync

提交回复
热议问题