如何等到从 Firebase 存储下载完成后再执行完成快速

发布于 2025-01-10 12:47:59 字数 1370 浏览 0 评论 0原文

在下面从 Firebase Storage 下载图像的模型中,它完成了 for 循环,但完成块中的 print 语句显示“[]”。然而,片刻之后,下载就到达了。显示在数组中。我如何等待图像完全下载&在执行完成之前进入数组稍后重新加载表格?

查看型号:下载图片


    //Firebase Cloud Storage Reference:
    let storage = Storage.storage()
    
    public var imageArray: [UIImage] = []

    public func fetchProductImages(completion: @escaping (Result<UIImage, Error>) -> Void) {
        
        //Clear Image Array:
        imageArray.removeAll()
        
        for imageRef in products! {
            
            //Access to Image inside a Collection:
            let storageRef = self.storage.reference(withPath: imageRef.image!)
            
            //Download in Memory with a Maximum Size of 1MB (1 * 1024 * 1024 Bytes):
            storageRef.getData(maxSize: 1 * 1024 * 1024) { [self] data, error in
                
                if let error = error {
                    //Error:
                    print (error)
                    
                } else {
                    
                    //Image Returned Successfully:
                    let image = UIImage(data: data!)

                    //Add Images to the Array:
                    imageArray.append(image!)
                    
                }
            }
        }
        
        print (imageArray)
        
        completion (true)
        
    }


In the below model that downloads the images from Firebase Storage it completes the for loop, but the print statement in the completion block shows "[]". However, moments later the downloads arrive & show in the array. How do I wait for the images to fully download & enter into the array before executing the completion & reload a table later?

View Model: Downloads Image


    //Firebase Cloud Storage Reference:
    let storage = Storage.storage()
    
    public var imageArray: [UIImage] = []

    public func fetchProductImages(completion: @escaping (Result<UIImage, Error>) -> Void) {
        
        //Clear Image Array:
        imageArray.removeAll()
        
        for imageRef in products! {
            
            //Access to Image inside a Collection:
            let storageRef = self.storage.reference(withPath: imageRef.image!)
            
            //Download in Memory with a Maximum Size of 1MB (1 * 1024 * 1024 Bytes):
            storageRef.getData(maxSize: 1 * 1024 * 1024) { [self] data, error in
                
                if let error = error {
                    //Error:
                    print (error)
                    
                } else {
                    
                    //Image Returned Successfully:
                    let image = UIImage(data: data!)

                    //Add Images to the Array:
                    imageArray.append(image!)
                    
                }
            }
        }
        
        print (imageArray)
        
        completion (true)
        
    }


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2025-01-17 12:47:59

由于加载图像数据是一个异步操作,因此在图像加载后需要运行的任何代码都需要位于完成处理程序内,从那里调用,或者以其他方式同步。

一个简单的方法是计算已完全加载的图像数量,并在与数组长度匹配时调用

imageArray.removeAll()

for imageRef in products! {

let storageRef = self.storage.reference(withPath: imageRef.image!)

storageRef.getData(maxSize: 1 * 1024 * 1024) { [self] data, error in

if let error = error {
print (error)
} else {

//Image Returned Successfully:
let image = UIImage(data: data!)

//Add Images to the Array:
imageArray.append(image!)

//

Since loading the image data is an asynchronous operation, any code that needs to run after the images have loaded will need to be inside the completion handler, be called from there, or be otherwise synchronized.

A simple way is to count the number of images that have completely loaded, and call once it matches the length of the array:

imageArray.removeAll()

for imageRef in products! {
    
    let storageRef = self.storage.reference(withPath: imageRef.image!)
    
    storageRef.getData(maxSize: 1 * 1024 * 1024) { [self] data, error in
        
        if let error = error {
            print (error)                
        } else {
            
            //Image Returned Successfully:
            let image = UIImage(data: data!)

            //Add Images to the Array:
            imageArray.append(image!)

            // ????
            if (imageArray.count == products!.count) {    
                completion (true)                
            }           
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文