ios - Firebase Storage 的 getMetaData() 是否调用后端(并因此引入较小的延迟)?

标签 ios swift firebase-storage

我目前使用以下 (Swift 4) 代码下载存储在 Firebase 存储上的图像:

func getImage(completion: @escaping (UIImage?)->()) {
    let ref = Storage.storage().reference().child("myImage.jpg")
    ref.getMetadata() {
        (metadata, error) in
        guard let url = metadata?.downloadURLs?.first, error == nil else {
            print(String(describing: error))
            completion(nil)
            return
        }
        //got url download image here and return it
        //this function is not important but it does it asynchroniously
        func downloadImageOrReturnACachedVersionOfItBy(url, completion)
    }

我注意到下载图像时有相当大的延迟,即使 downloadImageOrReturnACachedVersionOfItBy(...) 实际上负责图像兑现

问题:

  1. 函数 getMetadata() 每次调用时是否都会联系后端? 那么这会导致往返 Web 服务调用延迟吗?

  2. 如果是这样...为了避免这种情况,上传图像时,最好将返回的网址存储在 Firebase putData() 的元数据中本地返回该应用程序?这些下载网址是终身固定的吗?是否会自动失效?

最佳答案

1) Does the function getMetadata() contact the backend each time its called? And thus can this introduce a round-trip web service calling delay?

是的,这是因为下载URL可能已经改变(例如开发者撤销了它),所以我们需要重新获取它。

请注意,我们有一个 downloadURL() method它做了同样的事情,但您不必手动解析元数据。

2) If so...to avoid this, would it be a good idea store the returned url, when the image was uploaded, returned in the metadata from Firebase putData() locally in the app? Are these download url's fixed for life? Are the automatically being invalidated somehow?

我建议使用 FirebaseUI integration with SDWebImage ,它会自动缓存并显示这些图像。

// Reference to an image file in Firebase Storage
let reference = storageRef.child("images/stars.jpg")

// UIImageView in your ViewController
let imageView: UIImageView = self.imageView

// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")

// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)

关于ios - Firebase Storage 的 getMetaData() 是否调用后端(并因此引入较小的延迟)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109599/

相关文章:

objective-c - 自定义 UIMenuItem 仅第一次显示

ios - UIImageView 缩放以填充不剪辑 subview

ios - 取消选择表行删除索引行

ios - 允许仅部分用户访问 Firebase Storage Bucket

Flutter相机录制的视频与浏览器不兼容 - 如何转换为.mp4?

Nuxt JS 未定义 Firebase 存储 XMLHttpRequest 错误

ios - 在 iOS 应用程序中检查语言

ios - UITableView:我想使用一种可以轻松修改部分和行(添加、删除)的方法。

ios - 需要帮助将 bool 重写为三元运算符

ios - 在 Game Center 比赛中出现错误 : cancelled by host