I am trying to update an image array by downloading image from firebase database. But for some reason, after the array is updated inside the function, it is not updated in the viewdidload function. I am new to xcode. Any ideas?
var images:[UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
retrieveImage()
print(self.images)
}
func retrieveImage(){
let userID = Auth.auth().currentUser?.uid
ref.child("Images").observeSingleEvent(of: .value, with: { (snapshot) in
let userImage = snapshot.value as? NSDictionary
let imageURLArray = userImage?.allKeys
if userImage != nil{
for index in 0...userImage!.count-1{
let imageProfile = userImage![imageURLArray?[index]] as? NSDictionary
let imageURL = imageProfile!["url"]
let storageRef = Storage.storage().reference(forURL: imageURL as! String)
storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
print(error.localizedDescription)
} else {
let image = UIImage(data: data!)
self.images.append(image!)
}
}
}
}
}) { (error) in
print(error.localizedDescription)
}
}
asynchronouscall you need to wait until your images downloaded.