么此ChildEventListener无法从Firebase节

么此ChildEventListener无法从Firebase节

本文介绍了为什么此ChildEventListener无法从Firebase节点读取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Image链接中所示的ChildEventListener来查询Firebase节点

I have a query to firebase node with ChildEventListener shown in image link

现在,当我运行此代码

mMessageRef.child(ScrollAppUtils.firebaseId).child(child.key).orderByKey().limitToLast(1).addChildEventListener(object : ChildEventListener {

    override fun onCancelled(p0: DatabaseError?) {}

    override fun onChildMoved(p0: DataSnapshot?, p1: String?) {}

    override fun onChildChanged(p0: DataSnapshot?, p1: String?) {}

    override fun onChildAdded(p0: DataSnapshot?, p1: String?) {

        Logger.d("inside onChild Added")
        if (p0!!.hasChildren())
        {

        var lastMessage = p0.getValue(MessagesTwo::class.java)
        var dialog = DefaultDialog(child.key, user.metaData.profileImage, user.metaData.name, arrayListUser, lastMessage, 2)
        if (!listDialog.contains(dialog)) {

            listDialog.add(dialog)
            Logger.d(listDialog.size)
            dialogListAdapter.setItems(listDialog)
            dialogListAdapter.notifyDataSetChanged()
        } else {
            Logger.d("inside updateItemByID : ")
            Logger.json(Gson().toJson(dialog))
            dialogListAdapter.updateItemById(dialog)
            dialogListAdapter.notifyDataSetChanged()
        }
        }


    }

    override fun onChildRemoved(p0: DataSnapshot?) {}
    })

当未找到节点时此侦听器根本无法工作,我认为应该可以正常运行,因为ValueEventListener运行得很好,但是该侦听器存在其他问题,因此我不得不使用它听众.请建议我,我在这里做错了什么?

this listener simply doesn't work when a node not found, which I believe should work as ValueEventListener is working very well but I have some other problem with that listener so I am compelled to use this listener. Please suggest me what is I am doing wrong here?

推荐答案

不是位置(或查询)位置的每个子对象都调用onChildAdded方法.如果没有匹配的子节点,则不会调用onChildAdded.

The onChildAdded method is called for each child not of the location (or query). If there are no matching child nodes, onChildAdded will not be called.

如果要检测没有子节点的情况,则还需要使用ValueEventListener.

If you want to detect the situation where there are no child nodes, you'll (also) need to use a ValueEventListener.

另请参阅:

  • Firebase onChildAdded not called when there is no data (which this question is really a duplicate of)
  • Can I determine if query does NOT find any children with values within a specified range?
  • What happens if a firebase url doesnot exist and we try to add a listener to it?
  • Firebase queryOrderedbyChild doesn't return a Null value

这篇关于为什么此ChildEventListener无法从Firebase节点读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:36
查看更多