How to check if data is inserted in Room Database

后端 未结 7 2707
执念已碎
执念已碎 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:36

    I make like this(I use coroutines suspend):

    class SaveVehicle(val repository: RepositoryInterface) {
        suspend fun invoke(vehicleListItem: VehicleListItem): VehicleListItem = 
            with(vehicleListItem) {
               also {
                     repository.saveVehicle(vehicleListItem)
               }
            }
    }
    

    Kotlin Doc

    inline fun  T.also(block: (T) -> Unit): T
    

    Calls the specified function block with this value as its argument and returns this value.

    also is good for performing some actions that take the context object as an argument. Use also for actions that need a reference rather to the object than to its properties and functions, or when you don't want to shadow this reference from an outer scope.

    You can check my example in GitHub:

提交回复
热议问题