检查节点是否没有子节点

检查节点是否没有子节点

本文介绍了Firebase DB:检查节点是否没有子节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定Firebase数据库节点是否没有子节点?似乎获取数据的唯一方法是使用侦听器,并且仅当添加或删除某些内容时它们才会触发.换句话说,如果没有孩子,什么也不会开火.

How do I find out if a Firebase DB node has no children? Seems the only way to get data is with listeners, and they only fire when something is added or removed. In other words, if there are no children, nothing will fire.

推荐答案

您可以使用addValueEventListener,在onDataChange中,您将有某种方法可以检查没有孩子.
addValueEventListener之所以能够工作,是因为对该 docs 进行编码>

You can use addValueEventListener, and in onDataChange you will have some way to check no children.
The addValueEventListener will work because arcoding this docs

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        // As cricket_007 we also can use hasChildren or getChildrenCount
        if(!snapshot.hasChildren()){
            // db has no children
        }

        // OR this way
        if(snapshot.getChildrenCount() == 0){
            // db has no children
        }

        // OR this way
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            // db has no children
        }
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
    }
});

这篇关于Firebase DB:检查节点是否没有子节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:50