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 {
            } 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 Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.