Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda