title: How to use LiveData for searching?
body: <p>I have a ViewModel that has a MutableLiveData of an arraylist of class Course</p>
  <pre><code>private var coursesList: MutableLiveData&lt;ArrayList&lt;Course&gt;&gt; = MutableLiveData()
  </code></pre>
  <p>This coursesList is filled with data got from an API (by Retrofit): <code>coursesList.postValue(response.body())</code></p>
  <p>Now, a user can search for a course by its name. The function that I have for searching is that I iterate through the elements of the coursesList and check if its name is equal to what a user typed. It returns an arrayList with the courses that start with the name typed (this list is later sent to a fragment which passes it to an adapter to be shown in a recyclerview):</p>
  <pre><code>fun getCoursesList(): MutableLiveData&lt;ArrayList&lt;Course&gt;&gt; {
          return coursesList
  }
  
  fun searchCourses(searchString: String): ArrayList&lt;Course&gt; {
  
      val resultsList: ArrayList&lt;Course&gt; = ArrayList()
  
      if (getCoursesList().value == null) return resultsList
  
      if (getCoursesList().value!!.size &gt; 0) {
         
          for (course in getCoursesList().value!!.iterator()) {
              if (course.name.toLowerCase(Locale.ROOT).startsWith(searchString)) {
                  resultsList.add(course)
              }
          }
      }
      resultsList.sortBy { it.price }
      return resultsList
  }
  </code></pre>
  <p>This function works and all but my instructor asked me to use LiveData for searching without giving any additional hints on how to do that.</p>
  <p>So my question is how to use LiveData for searching? I tried to search for answers, I saw that some used LiveDataTransformations.switchMap but they were all using RoomDAOs and I couldn't adapt it to the code that I have.</p>
  <p>Any help would be appreciated very much. Thanks in advance.</p>