title: best way to convert flow to live data
body: <p>I am implementing search from Unsplash api and data will be updated on the basis of search</p>
  <p><strong>GalleryViewModel.kt</strong></p>
  <pre><code>@HiltViewModel
  class GalleryViewModel @Inject constructor(
      private val fetchPhotoUseCase:FetchPhotoUseCase,
      @Assisted state: SavedStateHandle
  ) :ViewModel(){
  
      companion object{
          private const val CURRENT_QUERY = &quot;current_query&quot; // key
          private const val DEFAULT_QUERY = &quot;cats&quot;
      }
  
      private val currentQuery = state.getLiveData(CURRENT_QUERY, DEFAULT_QUERY)
      
      val photos = currentQuery.switchMap { queryString -&gt;
          liveData {
              fetchPhotoUseCase.execute(queryString).collect {  
                  emit(it)
              }
          }
      }
  
     fun searchPhotos(query: String) {
      currentQuery.value = query
     }
  }
  </code></pre>
  <p><strong>FetchPhotoUseCase.kt</strong></p>
  <pre><code>class FetchPhotoUseCase @Inject constructor(
      private val repository: UnSplashRepository
  ) : UseCaseWithParams&lt;String,Flow&lt;PagingData&lt;UnsplashPhoto&gt;&gt;&gt;(){
      override suspend fun buildUseCase(params: String): Flow&lt;PagingData&lt;UnsplashPhoto&gt;&gt; {
          return repository.getSearchResult(params)
      }
  }
  </code></pre>
  <p><code>FetchPhotoUseCase</code> is in the domain layer. It returns flow so I change flow to live data in switchmap lambda.</p>
  <p>Am I doing it right or is there a better way to achieve it..</p>
  <p><strong>Edit</strong></p>
  <p>I have updated my code to work on both dispatchers like below..</p>
  <pre><code>suspend fun getPhotos(queryString: String) = withContext(Dispatchers.IO){
          fetchPhotoUseCase.execute(queryString)
      }
  
      fun getImages(queryString: String) = liveData(Dispatchers.Main) {
          getPhotos(queryString).collect {
              emit(
                  it.map { unsplash -&gt;
                      unSplashMapper.mapFromEntity(unsplash)
                  }
              )
          }
      }
  </code></pre>