gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:53:20 25 4
gpt4 key购买 nike

我目前使用以下 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com