java - Android rxJava 多响应
问题描述
我正在使用带有 rxjava 的 MVVM 架构,我试图从几个查询中获得一个响应,但它什么也没做,甚至不会引发错误
DAO中的查询方法
@Query("SELECT * FROM Notes WHERE type =:type")
fun getNotes(type: String): Single<List<Note>>
本地数据源
class NotesLocalDataSource private constructor(
private val notesDao: NotesDao
) : NotesDataSource {
override fun getNotes(): Single<Notes> {
return Single.zip(
notesDao.getNotes("typeOne"),
notesDao.getNotes("typeTwo"),
notesDao.getNotes("typeThree"),
notesDao.getNotes("typeFour"),
Function4 { t1, t2, t3, t4 ->
Notes(t1,t2,t3, t4)
})
}
NotesDataSource
interface NotesDataSource {
fun getNotes() : Single<Notes>
}
笔记模型
data class Notes(val typeOne: List<Note>, val typeTwo: List<Note>, val typeThree: List<Note>, val typeFour: List<Note>)
存储库
class NotesRepository(
private val notesLocalDataSource: NotesDataSource
) : NotesDataSource {
override fun getNotes(): Single<Notes> {
return notesLocalDataSource.getNotes()
}
}
视图模型
notesRepository.getNotes().map {
Log.e("TAG","Notes: $it")
}
解决方案
您必须subscribe()
进入该流才能开始生产项目。
只需subscribe()
在 ViewModel 或 Fragment/Activity 中调用函数
推荐阅读
- nginx - 如何使用 metricbeat 检查我的服务器是否还活着,这可能吗?
- angularjs - ng-repeat : 添加新元素的 ng-model 问题
- swift - 如果存在多个有效的登录用户,则无法调用 RealmSwift currentUser
- asp.net-core - 发送客户端请求后如何执行操作?
- r - 根据上年和今年汇总
- c# - 通过特定时间检查字符串值
- python - 如何更改列表中的元素位置
- go - 通过 ssh 在 64 位主机上构建 32 位:对象为 386,预期为 amd64
- php - 我需要将额外的值推送到会话中
- elasticsearch - ElasticSearch 中的 TRIM() 函数