Android rxJava多个响应

时间:2018-06-28 16:50:12

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

我正在将MVVM体系结构与rxjava一起使用,并且试图从几个查询中获得一个响应,但是它什么也不做,甚至不会引发错误

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

1 个答案:

答案 0 :(得分:1)

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

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