Remy Benza
05/05/2022, 7:27 AMRemy Benza
05/05/2022, 7:27 AMfun getLiveGameData(viewScope: CoroutineScope) {
viewScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
while (isActive) {
// make the 4 API calls concurrently
val fixture = async { payPerViewRepository.getNextFixture() }
val liveVideo = async { payPerViewRepository.getPPVLiveVideo()?.liveVideo }
val showLiveGame = async { payPerViewRepository.getPPVToggle()?.showVideo == 1 }
val liveMatchInfo = async {
// get id from Fixture first to make API call for live info
val matchId = fixture.await()?.id.orEmpty()
payPerViewRepository.getLiveMatchInfo(matchId)
}
val data = PayPerViewLiveGame(
liveVideo = liveVideo.await(),
fixture = fixture.await(),
liveMatchInfo = liveMatchInfo.await(),
showVideoPlayer = showLiveGame.await()
)
liveGameData.postValue(data)
delay(Duration.ofSeconds(10).toMillis()) // poll every 10 secs
}
}
Remy Benza
05/05/2022, 7:27 AMRemy Benza
05/05/2022, 7:28 AMliveMatchInfo
relies on the first API call that returns a fixture
objectRemy Benza
05/05/2022, 7:29 AMval liveVideo = async { payPerViewRepository.getPPVLiveVideo()?.liveVideo }
val showLiveGame = async { payPerViewRepository.getPPVToggle()?.showVideo == 1 }
val fixture = payPerViewRepository.getNextFixture() // suspend here to wait for result
val liveMatchInfo = async {
// get id from Fixture first to make API call for live info
val matchId = fixture.id.orEmpty()
payPerViewRepository.getLiveMatchInfo(matchId)
}
Remy Benza
05/05/2022, 7:30 AMpayPerViewRepository.getNextFixture()
returnsRemy Benza
05/05/2022, 7:32 AMkrzysztof
05/05/2022, 8:05 AMfixture
to same async call, where it is needed as argument?
val liveMatchInfo = async {
val fixture = payPerViewRepository.getNextFixture()
val matchId = fixture.id.orEmpty()
payPerViewRepository.getLiveMatchInfo(matchId)
}
Remy Benza
05/05/2022, 8:06 AMchristophsturm
05/05/2022, 8:09 AMkrzysztof
05/05/2022, 8:10 AMthen async no longer returns deferredNot exactly following, You mean call to
async {}
won’t return you Deferred?Remy Benza
05/05/2022, 8:11 AMkrzysztof
05/05/2022, 8:13 AMasync
tookrzysztof
05/05/2022, 8:15 AMchristophsturm
05/05/2022, 8:15 AMliveVideo
and showLiveGame
calls asyncRemy Benza
05/05/2022, 8:15 AMRemy Benza
05/05/2022, 8:15 AMchristophsturm
05/05/2022, 8:17 AMRemy Benza
05/05/2022, 8:18 AMchristophsturm
05/05/2022, 8:18 AMRemy Benza
05/05/2022, 8:19 AMchristophsturm
05/05/2022, 8:19 AMRemy Benza
05/05/2022, 8:20 AMliveVideo
and showLiveGame
are already running concurrently right?christophsturm
05/05/2022, 8:21 AMasync { xxx()} .await()
is the same as xxx()
Remy Benza
05/05/2022, 8:21 AMursus
05/05/2022, 10:21 AMRemy Benza
05/05/2022, 11:19 AMursus
05/05/2022, 11:42 AMconcurrent(a, b, sequential(c,d))
uli
05/06/2022, 10:53 AM