iOS - 如何使用“全部列出”按顺序下载文件

标签 ios swift firebase firebase-storage

我是 Firebase 新手,尝试从文件夹下载所有图像,同时尝试在 URL 数组中保持它们的顺序。

我使用函数 listAll 获取包含所有图像的引用数组,然后循环遍历它们,下载 URL 并将它们插入到 itemImagesURL 数组中。当我们循环遍历所有图像时,将使用 completionHandler 返回数组。

这样做的主要问题是数组以错误的顺序填充图像,因为每个项目都不会使用相同的 URL 来下载(并将其附加到数组中)。

有没有办法下载每个项目的 URL,同时保持 result.items 引用数组的原始顺序?

我的代码如下:

 func getItemImages(completionHandler: @escaping ([URL?]) -> Void) {

    var itemImagesURL: [URL] = []

    // We retrieve all images using listAll
    downloadRef.listAll(completion: { result, error in

        if let error = error {
            print("Error listing item images: ", error.localizedDescription)
            return
        }

        itemImagesURL.reserveCapacity(result.items.count)

        // We download every image's url
        for index in 0 ..< result.items.count {

             result.items[index].downloadURL(completion: { url, error in
                if let error = error {
                    //Handle any errors
                    print("Error downloading item image: ", error.localizedDescription)
                    return
                } else {
                    // Get download URL
                    itemImagesURL.insert(url!, at: index)

                    // All the urls of the item's images are retrieved -> we escape
                    if itemImagesURL.count == result.items.count {
                        print("Item's Images download finished.")
                        completionHandler(itemImagesURL)
                    }

                }
            })
        }

    })  // list all
}

}

最佳答案

这是一个想法。我通常发现我想与图像一起存储其他内容,例如图像名称、图像本身、发布日期等,因此我喜欢有一个类来保存这些项目。

class ImageInfoClass {
    var name = ""
    var url = "some url"
    var index: Int!
    var image: Image()
}

然后,存储它们的数组 - 例如可以用作 tableView 的数据源。

var myImageArray = [ImageInfoClass]()

然后从存储中读入它们...读入最后一个后,按索引排序即可完成设置。

func loadImagesAndSort() {
    for (index, imageUrl) in result.items.enumerated() {
        //perform download
        // for each image that's downloaded, create a ImageInfoClass object
        //   and populate with the image, the index and a image name for example
        let imageInfo = ImageInfoClass()
        imageInfo.name = name
        imageInfo.index = index
        imageInfo.image = //the image
        self.myImageArray.append(imageInfo)

        if index == lastImageIndex {
           self.myImageArray.sort(by: { $0.index < $1.index})
           //print to show they are sorted
           self.myImageArray.forEach { print($0.index, $0.name)}
        }
    }
}

关于iOS - 如何使用“全部列出”按顺序下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58400596/

相关文章:

objective-c - Swift 中的 NS_ENUM

arrays - 如何在 Swift 中对 NSDate 进行排序

iOS - 如何水平居中放置未知数量的不同尺寸的 subview 。

ios - 在不同设备上使用同一帐户数据不同步

ios - UIView 动画不重复/突然停止

iphone - 将 ImageView 设置为分组 uitableview 中部分的背景

ios - 删除 uicollectionview 顶部的额外空间

objective-c - 我在我的应用程序中使用 NSJSONSerialization,这个应用程序可以在 IOS 3.x 下正常运行吗?

swift - 从 Firebase 读取到 UIpickerView

Firebase 动态链接子域配置