I need to get a liveData from the return value of a suspend function. For this -
- I can launch a coroutine (using for eg viewmodelScope) and use postValue to update a MutableLiveData instance.
val apiLiveData = MutableLiveData<MenuItem?>()
fun getLiveData(): LiveData<MenuItem?> {
viewModelScope.launch {
apiLiveData.postValue(Repository.getMenuItem())
}
return apiLiveData
}
- I can use livedata {} and emit the returned value of my suspending function.
val apiLiveData: LiveData<MenuItem?> = liveData {
emit(Repository.getMenuItem())
}
Which of the above method should I use ?