Roger
Roger

Reputation: 63

Firebase Database Read childByAutoId

Im currently learning ropes in Firebase for iOS, so bare with my novice skills.

Below is a screenshot of my database:

gZ7dn2LMkiah is the autoId for a user
LDBTRFVS8dtz and LDBTe4oXs are autoIds for childId for this user

enter image description here

How can I read the two child nodes inside the node gZ7dn2LMkiah?
Cause from my code below, it can only be possible if I have only one child underneath this node, not two

ref = Database.database().reference()
    let userId: String = (Auth.auth().currentUser?.uid)!

    databaseHandle = ref?.child("childId").child(userId).observe(.childAdded, with: { (snapshot) in


I tried adding childByAutoId after child(userId) but it didn't return any results.

Any help would be appreciated. Thanks in advance.

Upvotes: 0

Views: 457

Answers (2)

TheTiger
TheTiger

Reputation: 13354

First of all your db seems incorrect. In first node nick and relation are inside the autoGeneratedKey and in second node nick and relation are outside the key as both are visible while the node is collapse. So these values should be inside autoGeneratedKey. Please change your db structure in correct way. See below screenshot:

enter image description here

This will be your childs table containing all childs for all parents and you can query for a particular parent to get his childs. See below code snippet:

 ref.child("childs").queryOrdered(byChild: "parentId").queryEqual(toValue: "123").observeSingleEvent(of: DataEventType.value) { (snapshot) in
     if snapshot.exists() {
         print("exists")
         for child in snapshot.children {
             let data = child as! DataSnapshot
             print(data.key)
             print(data.value)
         }
     }
     else {
         print("doesn't exist")
     }
 }

Output:

-LDBus9Xas3oTccwPN4r
Optional({
    nick = Dave;
    parentId = 123;
    relation = Son;
})
-LDBus9_Uz_qe69e9CXK
Optional({
    nick = Susan;
    parentId = 123;
    relation = Daughter;
})

Where parentId is let userId: String = (Auth.auth().currentUser?.uid)!.

Note 1: I tried adding childByAutoId after child(userId) but it didn't return any results.

Yes it will not work because childByAutoId generate a new key which will never match with existing db keys so you will get nothing.

Note 2: When to use .childAdded

.childAdded event is to listen new entry for the node reference not to fetch the data. To fetch the data for once we should use .observeSingleEvent event.

Note 3:Cause from my code below, it can only be possible if I have only one child underneath this node, not two

No its not possible. This is just because of second node's nick and relation are outside of the key.

Upvotes: 0

Jignesh Mayani
Jignesh Mayani

Reputation: 7193

Database.database().reference(withPath: 
 "childId").child(userId).observe(.childAdded)
   { (snapshot:DataSnapshot) in    
       // This will print all sub node  
      print(snapshot)
   }

Upvotes: 2

Related Questions