What is the recommended means of wrapping a MutableList
within either LiveData
or MutableLiveData
such that one can modify the list and observe the changes it undergoes ?
I have some fairly short tables (<20 items) in Room (It caches the values retrieved from a server). I would like to retain a mutable subset of these items; to be used as filters in queries for other, larger, tables. My thinking was to assign a MutableList
to the model class in my application and make it observable by wrapping this class in either MutableLiveData
or LiveData
. Then by updating the items in these selections I could trigger the necessary queries on the large tables.
Presently my code has the following structure, I've stripped it down in my question to what I thought was relevant. Eventually I would like to use multiple keyset
in more complex queries. My simplified model file is as follows,
class Model(application : Application) : AndroidViewModel(application)
{
val keys : LiveData<List<Key>>
val keyset : LiveData<MutableList<Key>>
val values : LiveData<List<Value>>
init {
keys = repository.keys
values = repository.values
keyset = mutableListOf<Key>() // I'm not entirely sure how to instantiate LiveData<MutableList<Key>>
}
}
it relies upon the repository,
class Repository(private val queryset : QuerySet)
{
val keys : LiveData<List<Key>> = queryset.getKeys()
val values : LiveData<List<Value>> = queryset.getValues()
}
Which relies upon the queryset
@Dao
class QuerySet(application : Application) : AndroidViewModel(application)
{
@Query("SELECT * FROM KeyEntity")
fun getKeys() : LiveData<list<Key>>
@Query("SELECT * FROM ValueEntity WHERE key in (:keys)")
fun getValues(keys : MutableList<Key>) : LiveData<list<Value>>
}
The Key
and Value
classes are simply the POJO for KeyEntity
and ValueEntity
and simply specify the same fields.
Should I be using MutableLiveData
or LiveData
for this purpose ? If so how should one implement keyset
? Perhaps there is a more canonical manner for doing so ?