UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

how to fix "out of range error" when trying to access array filled with downloaded images from firebase storage

Forums > SwiftUI

I tried to download successfully uploaded images to storage with the code:

func retrieveAllPictures(helloid: String) async {

    let db = Firestore.firestore()
    var retrievedEventImages = [UIImage]()

    let result = try? await db.collection("events").document(helloid).getDocument()
    allPictureIDs = (result?.data()!["pictures"] as? [String])!
    var image = UIImage()

    for path in allPictureIDs {
        let storage = Storage.storage().reference()
        let fileRef = storage.child(path)

  try? await fileRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
            if let error = error {
                return
            } else {
                image = UIImage(data: data!)!
               retrievedEventImages.append(image)
            }
        }
}

}

when trying to debug the code, I see that retrievedEventImages is filled with the UIImages still when trying to access the array at index 0 I get an out of range error maybe someone knows how to fix it, any help is appreciated

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.