0

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)
    }

}
2
  • 2
    Since its an asynchronous call you need to wait until your images downloaded. Commented Dec 7, 2018 at 8:44
  • add a completion handler for your retrieveImage function. Commented Dec 7, 2018 at 8:45

2 Answers 2

2

because observeSingleEvent runs asynchronously, the print function is called before observeSingleEvent finish. You can fix it like this, use closure

var images:[UIImage] = []
override func viewDidLoad() {
    super.viewDidLoad()
    ref = Database.database().reference()
    retrieveImage {
        print(self.images)
    }
}
func retrieveImage(_ completion: () -> Void){
    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!)

                }
            }

        }
    }
    completion()
}) { (error) in
    print(error.localizedDescription)
    completion()
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

becouse function storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in... is asynchronously. you can use dispath after some time to get images
0

you can use like this

var images:[UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
retrieveImage {
    print(self.images)
}
}

func retrieveImage(_ completion: () -> Void){
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 {
                    completion()
                    print(error.localizedDescription)
                } else {
                    let image = UIImage(data: data!)
                    self.images.append(image!)
                    if index == userImage!.count-1 {
                        completion()
                    }
                }
            }

        }
    } else {
        completion()
    }
}) { (error) in
    print(error.localizedDescription)
    completion()
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.