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;
}







0















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 ?










share|improve this question































    0















    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 ?










    share|improve this question



























      0












      0








      0








      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 ?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 10:34







      Carel

















      asked Nov 21 '18 at 23:08









      CarelCarel

      93211130




      93211130
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer


























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            0














            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.






            share|improve this answer






























              0














              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.






              share|improve this answer




























                0












                0








                0







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 13:30

























                answered Nov 22 '18 at 10:33









                CarelCarel

                93211130




                93211130
































                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    鏡平學校

                    ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                    Why https connections are so slow when debugging (stepping over) in Java?