Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
Firebase swift not retrieving all child values

I have a piece of code inside my Swift built iOS app, to retrieve all the nodes from a Firebase Realtime database. When I execute the code below I've noticed that it does not return all the child nodes.

When I query the particular nodes which are not being returned individually, at first the code returns 'nil' and then on a second attempt retrieves the nodes. (without doing any code changes in the process). Following this process, the node starts to show up in the results with the retrieve all nodes function.

Example 1: First returns nil, then on a second attempt returns the node. Which I can see from the console and definitely exists on the database.

ref?.child("transactions").child(email).child("14526452327").observeSingleEvent(of: .value, with: { (snapshot) in
                // Get user value
                let value = snapshot.value as? NSDictionary
                        print(value)
                        print("!!****************!!")
                // ...
            }) { (error) in
                print(error.localizedDescription)
            }

The following is being used to retrieve all child values; at first this doesn't get all the nodes, however after running the code from Example 1 (twice) it starts to return the node in question.

ref?.child("transactions").child(email).observeSingleEvent(of: .value, with: { (snapshot) in

        let childrenCount = snapshot.childrenCount
        var counter : Int = 0

        for trans in snapshot.children.allObjects as! [DataSnapshot]
        {
            counter = counter + 1

            self.ref?.child("transactions").child(email).child(trans.key).observeSingleEvent(of: .value, with: { (snapshot2) in

Here is a screenshot of the Firebase DB structure;

I've also checked my Firebase query and data limits and I am nowhere near the threshold for the free account. Any help is greatly appreciated.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this:

func getData() {
    // Making a reference
    let transactionRef = Database.database().reference(withPath: "transactions")
    transactionRef.observeSingleEvent(of: .value, with: { (snapshot) in

        // Printing the child count
        print("There are \(snapshot.childrenCount) children found")    

        // Checking if the reference has some values
        if snapshot.childrenCount > 0 {

            // Go through every child
            for data in snapshot.children.allObjects as! [DataSnapshot] {
                if let data = data.value as? [String: Any] {

                    // Retrieve the data per child


                    // Example
                    let name = data["name"] as? String
                    let age = data["age"] as? Int

                    // Print the values for each child or do whatever you want
                    print("Name: \(name)\nAge: \(age)")
                }
            }
        }
    })
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!