将项目添加到 Firebase 数据库中的列表 [英] Add an item to a list in Firebase Database

查看:22
本文介绍了将项目添加到 Firebase 数据库中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 Firebase 数据库结构.uIds 是一种 List.我正在尝试在 uIds 下添加另一个 uId 并增加索引.setValue()updateChildren() 需要我检索现有数据,push() 将添加一个随机生成的字符串作为项目一个键而不是一个递增的索引.有没有更简单的方法不需要检索现有数据?谢谢!

I have the following Firebase database structure. uIds is a type of List<String>. I am trying to add another uId under uIds with an incremented index. setValue() and updateChildren() would require me to retrieve existing data, and push() will add an item with a randomly generated string as a key instead of an incremented index. Is there a simpler way that does not require to retrieve the existing data? Thanks!

  "requests" : {
    "request001" : {
      "interests" : [ "x" ],
      "live" : true,
      "uIds" : [ "user1" ]  // <---- from this
    },
    "request002" : {
      "interests" : [ "y" ],
      "live" : true,
      "uIds" : [ "user2" ]
    }
  }

--------------------------------

抱歉,不清楚.让我详细说明一下.假设我有上面的数据库,并想将其更新为以下内容.

Sorry for the unclarity. Let me elaborate to make it clear. Say I have the above database and want to update it to the following.

  "requests" : {
    "-KSVYZwUQPfyosiyRVdr" : {
      "interests" : [ "x" ],
      "live" : true,
      "uIds" : [ "user1", "user2" ]   // <--- to this
    },
    "-KSl1L60g0tW5voyv0VU" : {
      "interests" : [ "y" ],
      "live" : true,
      "uIds" : [ "user2" ]
    }
  }

ishmaelMakitla 的建议,mDatabase.child("requests").child("request001").setValue(newRequest),将用newRequest"覆盖request001".所以我应该检索request001"的现有数据并将user2"添加到列表uIds.它会是这样的:

ishmaelMakitla's suggestion, mDatabase.child("requests").child("request001").setValue(newRequest), will overwrite the "request001" with "newRequest". So I should retrieve the existing data of "request001" and add "user2" to the list uIds. It will be something like this:

mDatabase.child("requests").child("request001").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Request newRequest = dataSnapshot.getValue(Request.class);
        newRequest.uIds.add("user2");
        mDatabase.child("requests").child("request001").setValue(newRequest);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}

});

但我想知道这个过程是否必要,因为我想做的只是将一项添加到列表 uIds 中.

But I am wondering if this process is necessary since what I am trying to do is simply to add one item to the list uIds.

推荐答案

有关创建可扩展数据的 Firebase 文档 建议您使用不同的数据结构:

The Firebase documentation on creating data that scales proposes that you use a different data structure:

"requests" : {
  "-KSVYZwUQPfyosiyRVdr" : {
    "interests" : { "x": true },
    "live" : true,
    "uIds" : { 
      "user1": true, 
      "user2": true 
    }
  },
  "-KSl1L60g0tW5voyv0VU" : {
    "interests" : { "y": true },
    "live" : true,
    "uIds" : { 
      "user2": true 
    }
  }
}

以下是该数据结构效果更好的一些原因:

Here are a few of the reasons why this data structure works better:

  • 每个 uid 现在只能自动出现一次.我们基本上将数据建模为集合,而不是使用数组.
  • 添加项目现在就像ref.child("uUids").child("user3").setValue(true)
  • 您现在可以检查安全规则中是否存在 uid.

我开始反复提醒自己:每当你发现自己在做 array.contains("xyz") 时,你可能应该使用集合而不是数组. 上面带有 "key": true 的映射是 Firebase 上集合的实现.

I have started re-iterating to myself: whenever you find yourself doing array.contains("xyz"), you should probably be using a set instead of an array. The above mapping with "key": true is an implementation of a set on Firebase.

有些人可能认为数组是一种更有效的数据存储方式,但在 Firebase 的情况下并非如此:

Some people may think arrays are a more efficient way of storing the data, but in the case of Firebase that is not true:

你看到的:

"uIds" : [ "user1", "user2" ]

Firebase 存储的内容:

What Firebase stores:

"uIds" : {
  "0": "user1",
  "1": "user2"
}

所以存储一个集合几乎是一样的:

So storing a set is pretty much the same:

"uIds" : { 
  "user1": true, 
  "user2": true 
}

这篇关于将项目添加到 Firebase 数据库中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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