如何实现嵌套的Value Event Listener从Firebase检索数据

时间:2018-10-29 15:35:05

标签: java android firebase firebase-realtime-database kotlin

我想通过嵌套的ValueEventListener从火力基地检索数据。 首先,我发现房间树中是否存在交易,之后是存在交易,然后我希望该交易具有折扣价值。

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

0 个答案:

没有答案