android - 如何使用 Kotlin 协程创建 Flow 请求
问题描述
我在我的应用程序中使用 Retrofit 和 Coroutines。
所以我的应用程序中有两个不同的请求。我想将它们与协程的 combine 结合起来。如果我想将两个请求相互结合,我需要它们是Flow
. 但是当我将我的 API 请求定义为 Flow 时,我会收到一个错误:
@GET("my_url")
fun getHomeDataFlow(@Header("Authorization") token: String): Flow<Pagination<Home>>
错误:
java.lang.IllegalArgumentException: Unable to create call adapter for kotlinx.coroutines.flow.Flow<codenevisha.ir.app.journey.data.model.response.Pagination<codenevisha.ir.app.journey.data.model.response.Home>>
for method APIService.getHomeDataFlow
这也是我的请求实现:
val homeFlow = oldRepository.getHomeDataFlow(params)
val categoryFlow = oldRepository.getCategoriesFlow()
var result = Pagination<Home>()
homeFlow.combine(categoryFlow) { receivedHome, receivedCategory ->
val home = Home()
home.setCategory(receivedCategory)
home.showType = Home.ShowType.CATEGORY.id
receivedHome.results?.add(1, home)
receivedHome
}.collect {
result = it
}
解决方案
推荐阅读
- java - 将查询中的 BIT 作为 Prepared Statement 中的参数传递
- angular - 从日期选择器将角度字符串转换为日期
- emacs - Convert a selected region to org table
- r - 引用特定值下的行,同时保持过滤器打开
- c++ - 语法问题:`char [5] const &`是什么意思?
- r - 从数据框中提取所有 8 位数字
- javascript - ChartJS 图表在添加新数据后出现错误
- go - 使用 big.Rat 和 Go 获取 Abs() 值
- ios-simulator - 无法在模拟器 MacOS Catalina 上打开应用程序
- javascript - 显示上次 ajax 调用值的表单