I'm trying to have a function that does all the API calls and Room storing. But in this function, I have one emit that doesn't work.
fun getAPI() = flow {
emit(State.Loading())
// suspend function
val response = safeApiCall{
// retrofit request here
}
when (response) {
is Success -> {
// Emit here not working
response.data?.let { emit(State.InsertOK(it)) }
// Insert data in Room
response.data?.let { insertQuery(it) }
}
is Error -> {
emit(State.Error(response.error)
}
}
emitAll(getQuery().map { State.Success(it) })
}
These values are collected in my ViewModel
private val _result = MutableLiveData<State<APIResult>>()
val result: LiveData<State<APIResult>>
get() = _result
fun getAPI( ) = viewModelScope.launch {
repository.getAPI()
.collect {
_result .postValue(it)
}
}
I tried to have my DAO call after and before my emit but nothing changes.
Am I doing something wrong with Flow that is not intended to be used like this?