I have a viewmodel for a fragment with a search functionality. I made use of Coroutines to fetch from the API and then set the MediatorLiveData value with the result, although the list of objects are reflected on my RecyclerView. When I try to access the MediatorLiveData value with liveData.value, it returns null. I've tried debugging it but despite displaying the list of objects, I can't really access the value of the livedata in the ViewModel
ViewModel
class SearchViewModel @Inject constructor(private val mutwitsRepo: MutwitsRepo) : BaseViewModel() {
  val query = MutableLiveData<String>()
  private val _isLoading = MutableLiveData<Boolean>()
  val isLoading: LiveData<Boolean> = _isLoading
  private val _users = MediatorLiveData<List<User>>()
  val users: LiveData<List<User>> = _users
  private var queryTextChangedJob: Job? = null
  init {
    _users.addSource(query) { queryStr ->
      if (queryStr.isNullOrEmpty()) _users.value = emptyList()
      else searchUsers(queryStr)
    }
  }
  fun searchUsers(query: String) {
    _isLoading.value = true
    queryTextChangedJob?.cancel()
    queryTextChangedJob = viewModelScope.launch {
      delay(300)
      _users.value = mutwitsRepo.searchUsers(query)
      _isLoading.value = false
    }
  }
  fun selectUser(user: User) {
    val temp = _users.value
    temp?.find { it.id_str == user.id_str }?.toggleSelected()
    _users.value = temp
  }
  override fun onCleared() {
    super.onCleared()
    queryTextChangedJob?.cancel()
  }
}
Repository Function
suspend fun searchUsers(query: String): List<User> = withContext(Dispatchers.IO) {
  return@withContext twitterService.searchUsers(query)
My codes are shown above, I've been stuck for days now and any help would be much appreciated! }
 
                        
Apparently this problem was caused by the injection of
ViewModelin my adapter. I injected my viewmodel in my adapter with dagger and set the button'sonClickListenerwith the functionselectUser(). I extracted the setting ofclickListenerto my fragment and it worked.