如何将用户添加到Firebase编号数组 [英] How to add a user to a firebase numbered array

查看:85
本文介绍了如何将用户添加到Firebase编号数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firebase的新手,正在尝试将用户添加到登录时的聊天组.我的函数当前使用childbyautoid将用户追加到成员",但这会添加Firebase生成的ID密钥和名为uid的密钥.

I am new to firebase and trying to add a user to a chat group on login. My function currently appends the user to 'members' by using childbyautoid but this adds a firebase generated ID key and key titled uid.

我想要做的是生成序列中的下一个数字作为键,并生成用户ID作为值.

What I want to do is generate the next number in the sequence as the key, and the user id as the value.

我在Swift中的功能:

My function in Swift:

let key = self.databaseRef.child("groups").child("-LEf3_zagKHB6qQEbTL7").child("members").childByAutoId().key
let newuser = ["uid": uid]
let childUpdates = ["/members/\(key)": newuser]
self.databaseRef.child("groups").child("-LEf3_zagKHB6qQEbTL7").updateChildValues(childUpdates)

当我尝试添加第四个 该组的成员,这是我在Firebase中获得的"LEf3_zagKHB6qQEbTL7"组的信息...

When I try to add a fourth member to the group, this is what I get in Firebase for the group "LEf3_zagKHB6qQEbTL7"...

{
  "description" : "The Tribe",
  "members" : {
    "0" : "5pw4MkVxKob8nXiHojTl61tQQ822",
    "1" : "FeP6H8H0PyVjMDcqwH3zbY0xAxg2",
    "2" : "ZtcfskmMydQmGIarYyKiuO6cKbh1",
    "-BdfJitm1T_fu1ssXcUY" : {
      "uid" : "dSiUpZfdRpXxE7WQL01T5lOUkfF2"
    }
  },
  "title" : "popupzendo"
}

我希望看到的是这个...

What I am expecting to see is this...

{
  "description" : "The Tribe",
  "members" : {
    "0" : "5pw4MkVxKob8nXiHojTl61tQQ822",
    "1" : "FeP6H8H0PyVjMDcqwH3zbY0xAxg2",
    "2" : "ZtcfskmMydQmGIarYyKiuO6cKbh1",
    "3" : "dSiUpZfdRpXxE7WQL01T5lOUkfF2"
    }
  },
  "title" : "popupzendo"
}

推荐答案

如果您要随后增加索引,则首先必须读取整个数组,确定下一个索引,然后在该索引处设置新子项,然后写回去.整个事务必须在事务中发生,以消除其他人在同一时间添加一个孩子并且他们都获得相同索引的机会.这也立即意味着,当用户间歇性地与服务器断开连接时,该操作将无法进行.

If you want to sequently increment the index, you'll first have to read the entire array, determine the next index, set the new child at that index, and then write it back. The entire thing will have to happen in a transaction to remove the chance of somebody else also adding a child around the same time and them both getting the same index. This immediately also means that the operation won't work when the user is intermittently disconnected from the server.

如果所有这些听起来都比预期的要棘手,您可能会意识到为什么Firebase的push()操作会生成不同类型的密钥.尽管它比顺序索引复杂得多,但它绕过了前面提到的所有问题:它不需要事务,没有冲突的机会,甚至不需要用户连接到服务器.

If all of this sounds like it's trickier than expected, you might realize why Firebase's push() operation generates a different type of key. While it's much more complex than a sequential index, it bypasses all the problems mentioned before: it requires no transaction, it has no chance of collisions, and it doesn't even require the user to be connected to the server.

要详细了解为什么数组索引难以与大型多用户方案结合使用,请阅读经典博客文章

For more on why array indices are tricky to combine with massively multi-user scenarios, read the classic blog post Best Practices: Arrays in Firebase.

如果您坚持使用数组索引,我建议您查看以下文章:

If you insist on using array indices, I recommend checking out these posts:

有关在Swift中处理Firebase中的列表的更多信息,请参见:

For more on handling lists in Firebase in Swift, see:

  • Adding items to Firebase array in Swift without observing for array first
  • Creating and appending arrays in Firebase-ios-swift

这篇关于如何将用户添加到Firebase编号数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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