简体   繁体   English

添加多个自定义元数据 Firebase Storage swift

[英]Add several custom metadata Firebase Storage swift

I'm trying to set multiple custom metadata to Firebase Storage on Swift我正在尝试在 Swift 上将多个自定义元数据设置为 Firebase 存储

my code is the following:我的代码如下:

let metadata = StorageMetadata()
        metadata.contentType = "image/jpg"
        metadata.customMetadata = ["lightVibrantColor" : "0"]
        metadata.customMetadata = ["imageWidth" : "\(updatedProfileImage.size.width)"]
        metadata.customMetadata = ["imageHeight" : "\(updatedProfileImage.size.height)"]
        metadata.customMetadata = ["darkMutedColor" : "0"]

However, only the last custom metadata is sent.但是,只会发送最后一个自定义元数据。 I know that because when I change the last custom metadata for another one, that is the one that is being sent.我知道这是因为当我为另一个更改最后一个自定义元数据时,就是正在发送的那个。

How can I add multiple custom metadata in order to send it to Firestore?如何添加多个自定义元数据以将其发送到 Firestore?

From the docs , the custom metadata can be set as文档中,自定义元数据可以设置为

let metadata = [
  "customMetadata": [
    "location": "Yosemite, CA, USA",
    "activity": "Hiking"
  ]
]

Instead of replacing the value of metadata again and again, you should use a dictionary.您应该使用字典,而不是一次又一次地替换元数据的值。

Seems that you're overriding the value.似乎您正在覆盖该值。 You need to change the code to:您需要将代码更改为:

let metadata = StorageMetadata()
metadata.contentType = "image/jpg"
metadata.customMetadata = ["lightVibrantColor" : "0",
"imageWidth" : "\(updatedProfileImage.size)",
"imageHeight" : "\(updatedProfileImage.size.height)",
"darkMutedColor" : "0"]

First, you have to make instance of metadata... then, you add values to changeable property "customMetadata".首先,您必须创建元数据的实例……然后,您将值添加到可更改的属性“customMetadata”。 After that, you connect that metadata to value that you have uploaded (in this case - image).之后,您将该元数据连接到您上传的值(在本例中为图像)。

    let metadata = StorageMetadata()
    metadata.customMetadata = ["key" : "value", "key" : "value"]
    let uploadTask = childImage.putData(data, metadata: metadata){metadata, error in
        guard let metadata = metadata else {
            if error != nil {
                print("Error")
            }
            return
        }
    }

Then you can access your metadata and use it.然后您可以访问您的元数据并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM