当我在RxJava中使用zip运算符来压缩2 Single时。 -我有Fixture对象和League对象,因为Api不会在Fixture中响应联盟的名称,所以我需要获取Fixture对象的id League,然后在League对象中进行查询。 -我的解决方案是我使用zip运算符来压缩治具和联赛的邮政编码,然后检查是否(治具中的LeagueId =联赛中的LeagueId)。最后,我设置了fixture.leagueName = League.name。但是问题是它的加载非常慢
Single.zip(repository.getScoresFixtures(finalDate),
leagueRepository.getAllLeagues(),
BiFunction<FixtureResponse, LeagueResponse, List<Fixture>> { t1, t2 ->
return@BiFunction getNameFixtureLeague(t1, t2)
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe {
_scoreFixtureLiveData.value = ApiResponse.loading()
}
.subscribe({
_scoreFixtureLiveData.value = ApiResponse.success(it)
}, {
_scoreFixtureLiveData.value = ApiResponse.error(it.message.toString())
})
还有这个
private fun getNameFixtureLeague(
fixtureResponse: FixtureResponse,
leagueResponse: LeagueResponse
): List<Fixture> {
val fixtures = fixtureResponse.data?.fixtures
val leagues = leagueResponse.data?.leagues
fixtures?.forEach {
leagues?.first { league -> league.id == it.leagueId }.apply {
it.leagueName = this?.name
}
}
return fixtures!!
}
我认为我的解决方案不好。有人可以帮我吗?