将项目添加到Firebase数据库的列表中 [英] Add an item to a list in Firebase Database
问题描述
我具有以下Firebase数据库结构. uIds
是List<String>
的一种.我试图在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
.
推荐答案
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现在只能自动出现一次.实际上,我们已将数据建模为 set 而不是使用数组.
- 添加项目现在与
ref.child("uUids").child("user3").setValue(true)
一样容易
- 您现在可以检查安全规则中是否存在uid.
- each uid can now automatically only appear once. We've essentially modeled our data as a set, instead of using an array.
- adding an item is now as easy as
ref.child("uUids").child("user3").setValue(true)
- you can now check if a uid exists in your security rules.
我已经开始对自己重复:只要您发现自己在做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屋!