HaVaNa7 HaVaNa7 - 26 days ago 9
Swift Question

firebase swift empty array out of observer

In my viewController I have an array of class Users named arrayUtenti, with the code below I need to add that user to that array but for some reason that i cannot understand I get an empty array, what could happened?

**************EDITED WORKS**************

func vengodaEditPremuto(completionHandler:@escaping ([Users]) -> ()){ self.root.child("users").child("ZJq98gvOEHaLUqYTbp4xF52f2K23").observeSingleEvent(of: .value, with: { (snapshot) in

var TempArray: [Users] = []


guard
let value = snapshot.value as? [String:AnyObject],
let userid = value["id"],
let username = value["name"],
let useremail = value["email"]
else {return}

let utenteesistente = Users(id: userid as? String, name: username as? String, email: useremail as? String)


TempArray.append(utenteesistente)



self.arrayUtenti = TempArray
completionHandler(self.arrayUtenti)
print(self.arrayUtenti) //here is ok

})

print(self.arrayUtenti) //here the array is empty!!
}

Answer Source

Your code looks fine after my comment. This is the last thing you need to do to make a correct call:

vengodaEditPremuto(){ result in
print(result)
}