Android rxJava 多重响应

Android rxJava multiple responses

我在 rxjava 中使用 MVVM 架构,我试图从几个查询中得到一个响应,但它什么也没做,甚至没有抛出错误

DAO中的查询方法

@Query("SELECT * FROM Notes WHERE type =:type")
fun getNotes(type: String): Single<List<Note>>

LocalDataSource

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()
    }
}

ViewModel

notesRepository.getNotes().map {
    Log.e("TAG","Notes: $it")
}

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

只需在 ViewModel 或 Fragment/Activity

中调用 subscribe() 函数