Reputation: 19190
I'm using Room to handle my local database, and LiveData to handle the DAOs.
So I'm using LiveData as thread handler for my transactions.
The question is how can I do insert and updates with LiveData? Or generally how can void functions return LiveData in the Room?
@Query("select * from table")
fun getAll(): LiveData<List<T>>
@Insert
fun insert(T data): LiveData<?> // What should be the generic, since it's void?
In RxJava we have something like this:
@Insert
fun insert(T data);
Upvotes: 2
Views: 1690
Reputation: 281
The question is how can I do insert and updates with LiveData? Or generally how can void functions return LiveData in the Room?
You can't use LiveData object itself to insert or update data in your database (db). LiveData is a wrapper around the object loaded from db which can be observed.
In order to perform insert/update operations you need to pass the object annotated with @Entity
annotation to your DAO
method parameter. You can do it either by calling LiveData#getValue()
, or observe the LiveData in Activity, or Fragment and update a local variable via the observer and pass said variable.
Upvotes: 1