Wrapping MutableList in either LiveData or MutableLiveData
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 ?
kotlin android-livedata mutablelivedata mutablelist
add a comment |
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 ?
kotlin android-livedata mutablelivedata mutablelist
add a comment |
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 ?
kotlin android-livedata mutablelivedata mutablelist
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 ?
kotlin android-livedata mutablelivedata mutablelist
kotlin android-livedata mutablelivedata mutablelist
edited Nov 22 '18 at 10:34
Carel
asked Nov 21 '18 at 23:08
CarelCarel
93211130
93211130
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my MutableList
as LiveData<MutablList<POJO>>
or as MutableLiveData<MutableList<POJO>>
, where POJO
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 the value
of keyset
is initialized as null
and one must set an initial value for the MutableLiveData
.
class Model(application : Application) : AndroidViewModel(application)
{ ...
val keyset : LiveData<MutableList<Key>>
init { ...
keyset = MutableLiveData<MutableList<Key>>() // Initialized as null
keyset.value = mutableListOf<Key>() // Assign an empty list
... }
... }
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.
Update LiveData objects
LiveData has no publicly available methods to update the stored data.
The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.
Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers.
Queries
What I have not worked out yet is how to trigger an update of the values
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421708%2fwrapping-mutablelist-in-either-livedata-or-mutablelivedata%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my MutableList
as LiveData<MutablList<POJO>>
or as MutableLiveData<MutableList<POJO>>
, where POJO
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 the value
of keyset
is initialized as null
and one must set an initial value for the MutableLiveData
.
class Model(application : Application) : AndroidViewModel(application)
{ ...
val keyset : LiveData<MutableList<Key>>
init { ...
keyset = MutableLiveData<MutableList<Key>>() // Initialized as null
keyset.value = mutableListOf<Key>() // Assign an empty list
... }
... }
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.
Update LiveData objects
LiveData has no publicly available methods to update the stored data.
The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.
Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers.
Queries
What I have not worked out yet is how to trigger an update of the values
.
add a comment |
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my MutableList
as LiveData<MutablList<POJO>>
or as MutableLiveData<MutableList<POJO>>
, where POJO
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 the value
of keyset
is initialized as null
and one must set an initial value for the MutableLiveData
.
class Model(application : Application) : AndroidViewModel(application)
{ ...
val keyset : LiveData<MutableList<Key>>
init { ...
keyset = MutableLiveData<MutableList<Key>>() // Initialized as null
keyset.value = mutableListOf<Key>() // Assign an empty list
... }
... }
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.
Update LiveData objects
LiveData has no publicly available methods to update the stored data.
The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.
Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers.
Queries
What I have not worked out yet is how to trigger an update of the values
.
add a comment |
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my MutableList
as LiveData<MutablList<POJO>>
or as MutableLiveData<MutableList<POJO>>
, where POJO
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 the value
of keyset
is initialized as null
and one must set an initial value for the MutableLiveData
.
class Model(application : Application) : AndroidViewModel(application)
{ ...
val keyset : LiveData<MutableList<Key>>
init { ...
keyset = MutableLiveData<MutableList<Key>>() // Initialized as null
keyset.value = mutableListOf<Key>() // Assign an empty list
... }
... }
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.
Update LiveData objects
LiveData has no publicly available methods to update the stored data.
The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.
Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers.
Queries
What I have not worked out yet is how to trigger an update of the values
.
LiveData vs. MutableLiveData
I was not sure whether I had to wrap my MutableList
as LiveData<MutablList<POJO>>
or as MutableLiveData<MutableList<POJO>>
, where POJO
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 the value
of keyset
is initialized as null
and one must set an initial value for the MutableLiveData
.
class Model(application : Application) : AndroidViewModel(application)
{ ...
val keyset : LiveData<MutableList<Key>>
init { ...
keyset = MutableLiveData<MutableList<Key>>() // Initialized as null
keyset.value = mutableListOf<Key>() // Assign an empty list
... }
... }
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.
Update LiveData objects
LiveData has no publicly available methods to update the stored data.
The MutableLiveData class exposes the setValue(T) and postValue(T) methods publicly and you must use these if you need to edit the value stored in a LiveData object.
Usually MutableLiveData is used in the ViewModel and then the ViewModel only exposes immutable LiveData objects to the observers.
Queries
What I have not worked out yet is how to trigger an update of the values
.
edited Nov 22 '18 at 13:30
answered Nov 22 '18 at 10:33
CarelCarel
93211130
93211130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421708%2fwrapping-mutablelist-in-either-livedata-or-mutablelivedata%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown