BLACK FRIDAY SALE: Save big on all my Swift books and bundles! >>

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 =
        let fileRef = storage.child(path)

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


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


Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

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

Reply to this topic…

You need to create an account or log in to reply.

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.