这是我的代码,如果它是ContentRow.CarouselRow,我将根据内容类型在homeContentRows上进行迭代,我会执行一个异步请求fetchAssets()
,该请求是已暂停的函数,并且正在使用suspendCoroutine函数,我不喜欢这样做遍历每个对象,并在对上一个对象完成时调用fetchAssets,我想并行处理它。我希望您能帮助我在这里介绍演员
homeContentRows.forEach { homeContentRow ->
uiScope.launch {
when (homeContentRow) {
is ContentRow.CarouselRow -> {
val carousel = homeContentRow.carousel
val assetsResult = assetRepository.fetchAssets(carousel.query)
val carouselRow = homeContentRow.copy(items = assetsResult.data)
contentRows.add(carouselRow)
contentRowMutableData.postValue(contentRows)
}
is ContentRow.HeroBannerRow -> {
contentRows.add(homeContentRow)
contentRowMutableData.postValue(contentRows)
}
}
}
override suspend fun fetchAssets(query: String): Response<List<Asset>> {
return suspendCoroutine { cont ->doHttp(assetsEndpoint,
object : JsonReaderResponseHandler() {
override fun onSuccess(jsonReader: JsonReader) {
val apiAsset = ApiAssetList(jsonReader)
cont.resume(Response.Success(apiAsset))
}
override fun onError(error: Error) {
cont.resume(Response.Failure("errorMessage"))
}
})
}
}