fun deal(room:String): Flowable<List<Deals>>? =
observeValueEvent(ref.child("rooms/$room/deals"))
.map { snapshot ->
snapshot.children.mapNotNull { child ->
var deal = child.key
if (deal != null) {
observeValueEvent(ref1.child("deals/$deal")).map { snapshot1 ->
snapshot1.children.mapNotNull { child1 ->
var name = child1.getValue().toString()
Deals(name)
}
}
}else{
null
}
}
}
这是从交易中获取价值的功能。 对于ValueEventListener,我做了不同的功能
private fun observeValueEvent(query: Query): Flowable<DataSnapshot> = Flowable
.create({ emitter ->
val valueEventListener = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
emitter.onNext(snapshot)
}
override fun onCancelled(error: DatabaseError) {
if (!emitter.isCancelled) {
emitter.onError(Throwable(error.details))
}
}
}
emitter.setCancellable { query.removeEventListener(valueEventListener) }
query.addValueEventListener(valueEventListener)
}, BackpressureStrategy.LATEST)
我尝试过这种方法,但是会给出错误
"mismatch type , required: Flowable<List<Deals>>, Found: Flowable<any>"