0

enter image description here

I want to fetch data from multiple subnodes? I am able to fetch from the first subnode, but unable to get the next subnode level. I need only the account section from the firebase

I used this function to fetch the first subnode.

func compareAccount() {
        REF_DRIVERS.observeSingleEvent(of: .value) { (snapshot) in
            print(snapshot)
        }
    }

Snap (drivers) {
    9AyIYcizavM03ArvmQH1e3xQ7rD3 =     {
        account = Default;
        driverIsOnTrip = 0;
        isPickupModeEnabled = 0;
        provider = Firebase;
        userIsDriver = 1;
    };
    FCuTwkhtrbdBTx7akQI4NL49K5p1 =     {
        account = Default;
        coordinate =         {
            0 = "37.33233141";
            1 = "-122.0312186";
        };
        driverIsOnTrip = 0;
        isPickupModeEnabled = 0;
        provider = Firebase;
        userIsDriver = 1;
    };
    ML2Sxz6l78ZIZ0mO327nGmh0YkP2 =     {
        account = Default;
        driverIsOnTrip = 0;
        isPickupModeEnabled = 0;
        provider = Firebase;
        userIsDriver = 1;
    };
}

func compareAccount() {
        REF_DRIVERS.child("account").observeSingleEvent(of: .value) { (snapshot) in
            print(snapshot)
        }
    }

When I try to print out only the account. It prints this result

Snap (account)

1 Answer 1

1

When you listen to a node, you get a snapshot with all data from that node. If that snapshot contains children for which you don't know the key (the driver IDs in your case), you can loop over all child nodes. If you do know the key of a child node (as for example the account property), you can use childSnapshot(forPath:) to get that node.

So by combining these, you can loop over your drivers and look up their properties by name:

REF_DRIVERS.observeSingleEvent(of: .value) { (snapshot) in
    for case let driverSnapshot as DataSnapshot in snapshot.children {
       print(driverSnapshot.key)
       print(driverSnapshot.childSnapshot(forPath:"account").value)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.