如何获取现值Firebase的嵌套根名称? [英] How to get Nested Root Names for Present Value Firebase?

查看:77
本文介绍了如何获取现值Firebase的嵌套根名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果节点中存在指定的 myid 值,我将尝试获取根节点名称.

I am trying to get Root Node Names if specified myid value is present inside the node.

例如:考虑以下结构

-Likes
    -uid1
        -postid1
            -myid
            -othersid1
            -othersid2
        -postid2
            -othersid1
            -othersid2
    -uid2
        -postid3
            -myid
            -othersid2

如果节点中存在 myid 值,我想获取 uid1 postid1 根名.如果该节点内存在 myid ,则同样获得 uid2 postid3 根名.

I want to get uid1 and postid1 rootnames if myid value is present inside the node. Similarly get uid2 and postid3 rootnames if myid present inside that node.

我这样写.

public void checkNode()
    {
        DatabaseReference reference=FirebaseDatabase.getInstance().getReference().child("Likes");
        reference.addValueEventListener(new ValueEventListener() {

            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds1: dataSnapshot.getChildren())
                    for(DataSnapshot ds2: ds1.getChildren())

                        if(ds2.hasChild("myid"))
                           Log.i("app","present inside: "+ds1.getKey()+" > "+ds2.getKey());
            }

            public void onCancelled( DatabaseError databaseError) {

            }
        });
    }

此代码正常工作.但是使用嵌套的for循环有效吗?如果效率不高,我该如何更高效地编写此代码?

This code works correctly. But Is it efficient to use nested for loops? How do i write this code more efficiently if not efficient?

推荐答案

要使用此代码解决问题

FirebaseDatabase.getInstance().getReference("/Likes/").orderByChild("myid").equalTo("some value").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
/**
 * -Likes
 *     -uid1
 *         -postid1
 *             -myid
 */
                    //You are at myid ref
                    //get postid reference like below
                    DatabaseReference postIdRef = dataSnapshot.getRef().getParent();
                    //uidRef
                    DatabaseReference uidRef = postIdRef.getParent();
                    //likesRef
                    DatabaseReference likesRef = uidRef.getParent();

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


一旦您的代码点击了onDataChanged(DataSnapshot snapshot)的代码块 您可以使用快照,然后依次调用getRef()getParent()方法


Once your code hit the code-block of onDataChanged(DataSnapshot snapshot) You can use snapshot and then call getRef() and then getParent() methods

一旦获得父节点引用,就可以使用getChilderen()

once you get parent node reference you can use that to traverse in the down tree using getChilderen()

一旦您的代码点击了onDataChanged(DataSnapshot snapshot)的代码块 您可以使用快照,然后依次调用getRef()getParent()方法

Once your code hit the code-block of onDataChanged(DataSnapshot snapshot) You can use snapshot and then call getRef() and then getParent() methods

一旦获得父节点引用,就可以使用getChilderen()

once you get parent node reference you can use that to traverse in the down tree using getChilderen()

唯一要记住的是,您的FirebaseUser应该对此父节点具有读取权限,否则您将获得异常.

The only thing to keep in mind is that your FirebaseUser should have read permission to this parent node else you will get an exception.

这篇关于如何获取现值Firebase的嵌套根名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆