0

I am trying to get the data from the database in firebase. I want to get the names and store them in an empty array. But the names are being retrieved one by one. how do I put them all in the array at once.

Firebase Database

var text = "abc"
var Files = [String]()

var MessageText = "" 

let databaseRef = Database.database().reference()
    
    databaseRef.child("Hawalli").queryOrderedByKey().observe(.childAdded, with: {
        snapshot in
        
        let snapshotValue = snapshot.value as? NSDictionary
        let alerts = snapshotValue?["Name"] as? String
        
        let MessageText = alerts
        
        self.text = MessageText!
        print("List of names")
        print(MessageText)
    })

output

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You can use `Files.append(alerts)` or `Files.append(MessageText)` – Emre Değirmenci Sep 07 '20 at 10:55
  • To get al child nodes in a single callback, use `.observe(.value` and then loop over `snapshot.children`. See for an example: https://stackoverflow.com/questions/56638755/get-the-data-from-all-children-in-firebase-using-swift/56640036#56640036 – Frank van Puffelen Sep 07 '20 at 14:00

0 Answers0