【问题标题】:Android rxJava multiple responsesAndroid rxJava 多响应
【发布时间】:2018-06-28 16:50:11
【问题描述】:

我正在使用 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")
}

【问题讨论】:

    标签: java android kotlin rx-java android-room


    【解决方案1】:

    您必须subscribe() 到该流才能开始生成项目。

    只需在 ViewModel 或 Fragment/Activity 中调用 subscribe() 函数

    【讨论】:

    • 谢谢它现在有效!我以正确的方式使用它的架构怎么样?我的意思是这个 Single.zip 和 .map?
    • @kosas 就 rxjava 而言,这是非常基础的知识。阅读您正在使用的任何库的文档对您很有帮助,以避免将来出现此类问题
    • 是的,这就是普通运算符的简单使用 + MVVM 与 Repository 模式
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2018-10-26
    • 2018-10-08
    • 2015-12-13
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多