Kotlinx.coroutines: Deferred result is never used

Created on 29 Jun 2018  路  2Comments  路  Source: Kotlin/kotlinx.coroutines

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

Most helpful comment

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()
    }
}

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mariusstaicu picture mariusstaicu  路  3Comments

mvysny picture mvysny  路  3Comments

mhernand40 picture mhernand40  路  3Comments

elizarov picture elizarov  路  3Comments

elizarov picture elizarov  路  3Comments