首页 > 解决方案 > 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")
}

标签: javaandroidkotlinrx-javaandroid-room

解决方案


您必须subscribe()进入该流才能开始生产项目。

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


推荐阅读