Android Firebase:嵌套数组数据中的更新/添加记录 [英] Android Firebase : Update/Add record in Nested Array data

查看:92
本文介绍了Android Firebase:嵌套数组数据中的更新/添加记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了Android中Firebase操作的一种情况. 我的要求:

I am stuck in one case of firebase operation in android. My Requirement :

我的表名为:值" 该值表包含以下数据类型.

I have table Named : "Values" This value table contains following type of data.

  {
    "menu": [
             {
               "category": "cocktails",
               "product": [
                            {
                              "id": "123",
                              "name": "test1"
                            },
                            {
                              "id": "456",
                              "name": "test2"
                            }
                          ]
            },
            {
               "category": "vodka",
               "product": [
                            {
                               "id": "789",
                               "name": "test3"
                             },
                             {
                                "id": "901",
                                "name": "test4"
                             }
                         ]
              }
           ]
       }

现在,我想用id ="test5"更新id:123,如何更新此嵌套数组对象 在Android的Firebase数据库中?

Now, I want to update id : 123 with name = "test5" , How to update this nested array object in firebase database in android?

我尝试了以下操作,但是它更新/添加了整个菜单数组,而不是特定的产品数组对象.

I tried following but it update/add Entire menu array not perticular product array object.

我只想更新产品数组对象.

I just want to update in product array object.

以下是我尝试过的代码,但它会更新所有菜单数组.

Following are the code that i tried, but it update all menu array.

      val listPorduct = ArrayList<HashMap<String, Any>>()

      for ((i, product) in productList.withIndex()) {

            var productMap = HashMap<String, Any>()

              productMap = hashMapOf(
                "id" to "123",
                "name" to "test5")

             listPorduct.add(productMap)
       }

        val map = hashMapOf<String, Any>()
        map.put("category", list[position].category)
        map.put("product", listPorduct)

       repository.getTabs().update("menu", FieldValue.arrayUnion(map))

如果我尝试

       repository.getTabs().update("menu/product", FieldValue.arrayUnion(map))

       or

       repository.getTabs().update("menu.product", FieldValue.arrayUnion(map))

然后出现问题/或不允许点.我有最新的firebase gradle文件.

then getting issue / or dot not allowed. i have latest firebase gradle file.

如何更新产品对象的特定位置? 请谁能帮我解决这个问题?

How update particular position of product object? Please anyone can help me to solve out this?

firebase数据库的图像.

Image of firebase database.

推荐答案

此问题的解决方案可以在以下帖子的答案中找到:

The solution to this problem can be found in my answer from the following post:

差异略小.但是,对于您的用例,请注意,您作为示例提供给我们的文档包含名为menu的属性,该属性的类型为array.在此数组中,添加了一些对象,这些对象包含两个属性,一个名为category的String属性和一个名为product的数组.该数组在术语上包含另外两个属性,一个是id,另一个是name,两者均为String类型.

There is a slightly smaller difference. However, for your use-case, please note that the document that you gave us as an example contains a property named menu which is of type array. In this array, you have added some objects that contain two properties, a String property named category and an array named product. This array contains in term two other properties, one is the id and the other one is the name, both being of type String.

使用FieldValue.arrayUnion(map)不能更新这些阵列.您唯一的选择是获取menu数组作为HashMap<String, Any>的列表.有了此地图后,您可以简单地对其进行迭代并获取所需的数据.

None of those arrays can be updated using FieldValue.arrayUnion(map). The only option that you have is to get the menu array as a list of HashMap<String, Any>. Once you have this map, you can simply iterate through it and get the desired data.

因此,要更新Map中某个属性的值,您应该首先获取地图,进行更改并将其放回去.最后,完成所有必要的更改后,将文档写回到Firestore.

So to update the value of a property inside a Map, you should first get the map, do the changes and put it back. In the end, after you've done all the necessary changes, write the document back to Firestore.

根据您的评论:

就我而言,列表中具有嵌套数组的记录超过5000条.

In my case I have more than 5000 records in a list with a nested array.

很难相信您可以将5000个元素嵌套到一个文档中,因为文档的最大大小为1 MiB(1,048,576字节),这可能不合适.请参阅用法和限制.因此,将5000个对象嵌套到数组中根本不是解决方案.在这种情况下,您唯一的解决方案是使用子集合并将数组中的每个项目添加为文档.在这种情况下,没有任何限制.

It's hard to believe that you can nest 5000 elements into a single document because it might not fit since the maximum size for a document is 1 MiB (1,048,576 bytes). Please see usage and limits. So nesting 5000 objects into an array isn't a solution at all. In this case, the single solution that you have is to use a sub-collection and add each item from the array as a document. In this case, there are no limitations.

结论是,存储未知的大量项目的唯一高度可伸缩的方法是使用集合中的文档.数组类型字段无法适应列表的不断增长,因为列表最终会超过该范围(1 MiB),这显然会在将来引起问题.

As a conclusion, the only highly scalable way to store an unknown large list of items is using documents in a collection. Array type fields do not scale for lists the are constantly growing because the items will eventually exceed that (1 MiB), which will obviously cause problems in the future.

这篇关于Android Firebase:嵌套数组数据中的更新/添加记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆