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 ?
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my
MutableList
asLiveData<MutablList<POJO>>
or asMutableLiveData<MutableList<POJO>>
, wherePOJO
is an instance of either a database entity or a data class proxying one or more database entities. Sneh Pandaya succinctly explains the difference between the two classes. I was also unsure as to how to instantiate them. The following creates the instance but thevalue
ofkeyset
is initialized asnull
and one must set an initial value for theMutableLiveData
.There is a nuance in
MutableLiveData
, it does not formally encapsulate the data. That is when it wraps an item it does not expose the wrapped items methods as it's own emitting notifications as the wrapped item is modified. It merely tracks when the wrapped item has been swapped out (See Gznlt). Samnang CHEA provides a nice method for supporting this.Location, Location, Location, ...
I was unsure of where to formally place the
keyset
attribute. The Android documentation provides the following information; as it turns out I was on the correct path.Queries
What I have not worked out yet is how to trigger an update of the
values
.