从数据库中查询数据并返回一个函数Flowable
@Dao
interface DataDao {
@Query("SELECT * FROM DataTable ORDER BY `order`")
fun listenToAll():Flowable<List<DataRoomEntity>>
}
订阅它时,一次性没有解决(见截图)。
var dataDao: DataDao
fun listenToDataDb(): Flowable<Data> = dataDao.listenToAll()
.map { convertEntitiesToData(it) }
fun getData(){
//var disposable: Disposable? = null
var disposable = listenToDataDb()
.subscribe({ data ->
// do something with data
disposable?.dispose() //<== disposable is unresolved
}, { throwable ->
disposable?.dispose(). //<== disposable is unresolved
})
}
}
但是如果确实定义了var disposable: Disposable = null
第一个就可以了
fun test(){
var disposable: Disposable? = null
disposable = listenToDataDb()
.subscribe({ data ->
// do something with data
disposable?.dispose() //<=== it is fine
}, { throwable ->
disposable?.dispose()
})
}
如果不先定义它为什么会unresolved
出错?