Hi
I'm have code like this
fun getMatchDetail(matchId: String?, homeTeamId: String?, awayTeamId: String?) {
view.showLoading()
async(UI) {
val data = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getMatchDetail(matchId)),
DetailEventResponse::class.java)
}
val homeTeamData = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getHomeTeam(homeTeamId)),
DetailHomeTeamResponse::class.java)
}
val awayTeamData = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getAwayTeam(awayTeamId)),
DetailAwayTeamtResponse::class.java)
}
view.showMatchDetail(data.await().matchDetail, homeTeamData.await().detailHomeTeam,
awayTeamData.await().detailAwayTeam)
view.hideLoading()
}
}
and i get hint from Android Studio _Deferred result is never used_ ,how to handle it ? i'm using plugin 1.2.50
Use val deferred = async {} when you care about the result an plan to call deferred.await().
Use launch when you want to fire and forget a job.
Personnaly I would write your code like this:
fun getMatchDetail(matchId: String?, homeTeamId: String?, awayTeamId: String?) {
view.showLoading()
val data = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getMatchDetail(matchId)),
DetailEventResponse::class.java)
}
val homeTeamData = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getHomeTeam(homeTeamId)),
DetailHomeTeamResponse::class.java)
}
val awayTeamData = bg {
gson.fromJson(apiRepository.doRequest(TheSportDBApi.getAwayTeam(awayTeamId)),
DetailAwayTeamtResponse::class.java)
}
launch(UI) {
view.showMatchDetail(data.await().matchDetail, homeTeamData.await().detailHomeTeam, awayTeamData.await().detailAwayTeam)
view.hideLoading()
}
}
Thank You! @jcornaz
Most helpful comment
Use
val deferred = async {}when you care about the result an plan to calldeferred.await().Use
launchwhen you want to fire and forget a job.Personnaly I would write your code like this: