带有改造和协程的 android 并行 API 请求

问题描述 投票:0回答:0

我有多个需要并行调用的API请求,顺序无关紧要。 真正重要的是应该请求所有调用来实现 UI。 问题是有时某些请求不会被调用,这会返回空值,换句话说,不保证所有请求都会被调用。 我已经阅读了很多关于并行 API 请求和启动器的内容,但仍然不知道我错过了什么或做错了什么。

这是我的视图模型类,具有所有功能

@HiltViewModel
class MatchDetailsViewModel @Inject constructor(
    val api:FootballApi,
    val app: Application,
): AndroidViewModel(app) {
    private val _matchDetailsMutableLiveData = MutableLiveData<ResponseState<FixtureById>>()
    private val _matchStatsMutableLiveData = MutableLiveData<ResponseState<Stats>>()
    private val _matchLineupsMutableLiveData = MutableLiveData<ResponseState<Lineups>>()
    private val _matchBenchMutableLiveData = MutableLiveData<ResponseState<Bench>>()
    private val _matchSideLinesMutableLiveData = MutableLiveData<ResponseState<SideLine>>()
    private val _matchStandingsMutableLiveData = MutableLiveData<ResponseState<Standings>>()
    val matchStandingsLiveData: LiveData<ResponseState<Standings>> = _matchStandingsMutableLiveData
    val matchSideLinesLiveData: LiveData<ResponseState<SideLine>> = _matchSideLinesMutableLiveData
    val matchStatsLiveData: LiveData<ResponseState<Stats>> = _matchStatsMutableLiveData
    val matchDetailsLiveData: LiveData<ResponseState<FixtureById>> = _matchDetailsMutableLiveData
    val matchLineupsLiveData: LiveData<ResponseState<Lineups>> = _matchLineupsMutableLiveData
    val matchBenchLiveData: LiveData<ResponseState<Bench>> = _matchBenchMutableLiveData


fun callAll() {
    viewModelScope.launch {
        val getMatchDetailsCall = async { getMatchDetails(1582601) }
        val getMatchStatsCall = async { getMatchStats(1582601) }
        val getMatchLineupsCall = async { getMatchLineups(1582601) }
        val getMatchBenchCall = async { getMatchBench(1582601) }
        val getMatchSideLineCall = async { getMatchSideLine(15006543) }
        val getMatchStandingsCall = async { getMatchStandings(12880) }
        try {
            getMatchDetailsCall.await()
            getMatchStatsCall.await()
            getMatchLineupsCall.await()
            getMatchBenchCall.await()
            getMatchSideLineCall.await()
            getMatchStandingsCall.await()
        }
        catch (_: Exception){}
    }
}

suspend fun getMatchStandings(seasonId: Int) = viewModelScope.launch(Dispatchers.IO) {
    _matchStandingsMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchStandings(seasonId = seasonId)
        Log.i("getMatchStanding()", response.body().toString())
        _matchStandingsMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    }
    catch (exception: Exception) {
        Log.e("getMatchStanding()", exception.toString())
    }
}

suspend fun getMatchSideLine(id: Int) = viewModelScope.launch(Dispatchers.IO) {
    _matchSideLinesMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchSideLines(id = id)
        Log.i("getMatchSideline()", response.body().toString())
        _matchSideLinesMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    } catch (exception: Exception) {
        Log.e("getMatchSideline()", exception.toString())
    }
}

fun getMatchDetails(id: Int) = viewModelScope.launch(Dispatchers.IO) {
    _matchDetailsMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchDetails(id = id)
        Log.i("getMatchDetails()", response.body().toString())
        _matchDetailsMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    } catch (exception: Exception) {
        Log.e("getMatchDetails()", exception.toString())
    }
}

suspend fun getMatchStats(id: Int) = viewModelScope.launch(Dispatchers.IO) {
    _matchStatsMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchStats(id = id)
        Log.i("getMatchStats()", response.body().toString())
        _matchStatsMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    } catch (exception: Exception) {
        Log.e("getMatchStats()", exception.toString())
    }
}

suspend fun getMatchLineups(id: Int = 6) = viewModelScope.launch(Dispatchers.IO) {
    _matchLineupsMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchLineups(id = id)
        Log.i("getMatchLineups()", response.body().toString())
        _matchLineupsMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    } catch (exception: Exception) {
        Log.e("getMatchLineups()", exception.toString())
       
    }
}

suspend fun getMatchBench(id: Int) = viewModelScope.launch(Dispatchers.IO) {
    _matchBenchMutableLiveData.postValue(ResponseState.Loading())
    try {
        val response = api.getMatchBench(id = id)
        Log.i("getMatchBench()", response.body().toString())
        _matchBenchMutableLiveData.postValue(ResponseState.Success(response.body()!!))
    } catch (exception: Exception) {
        Log.e("getMatchBench()", exception.toString())
    }
}
}

这是我在活动课上的电话

lifecycleScope.launch(Dispatchers.IO) {
        matchDetailsViewModel.callAll()
    }
android kotlin viewmodel kotlin-coroutines
© www.soinside.com 2019 - 2024. All rights reserved.