简体   繁体   English

如何将多个图像 URL 添加到 Cloud Firestore 中的同一字段

[英]How to add multiple image URLs to the same field in Cloud Firestore

I am new to coding and I am looking for a solution to add multiple image URLs (the images are selected from the gallery) to the same field in Cloud Firestore so as I could retrieve and display them later in a ViewPager .我是编码新手,我正在寻找一种解决方案,将多个图像URLs (图像是从图库中选择的)添加到 Cloud Firestore 中的同一字段,以便我稍后可以在ViewPager检索和显示它们。

I have been trying to find the solution, but with no luck.我一直试图找到解决方案,但没有运气。

Please, help me.请帮我。

Thank you in advance.先感谢您。

您可以通过将 json 数组存储在 cloud_firestore image_url_key {"array": ["url1","url2","url3"]} 中来尝试

There are two ways in which you can achieve this.有两种方法可以实现这一目标。 The first one would be to store those urls in a map .第一个是将这些 url 存储在map In this case the database structure should look this:在这种情况下,数据库结构应如下所示:

Firestore-root
   |
   --- collection
         |
         --- document
               |
               --- urls
                    |
                    --- url : "http://..."
                    |
                    --- url : "http://..."

But because the urls contain characters that aren't allowed to be used in the keys, you'll need to use an encoded version of the URL as the key (typically just strips the illegal characters, but a hashcode is also common) and then use the full URL as the value.但是因为 url 包含不允许在键中使用的字符,所以您需要使用 URL 的编码版本作为键(通常只是去除非法字符,但哈希码也很常见),然后使用完整的 URL 作为值。

Because from 13th august it is possible to filter data in Cloud Firestore based on array values using whereArrayContains() , the second solution would be to store those urls in an array.因为从 8 月 13 日起,可以使用whereArrayContains() 根据数组值过滤 Cloud Firestore 中的数据,所以第二种解决方案是将这些 url 存储在数组中。 In this case the database structure should look this:在这种情况下,数据库结构应如下所示:

Firestore-root
   |
   --- collection
         |
         --- document
               |
               --- urls: ["http://...", "http://..."]

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

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