Reputation: 3637
I'm having a chat app where the users could set 1:1 or 1:many chats rooms. I want to store all uid
s for that chat_room
under chat_room_members
. I'm trying to use updateChildValues([user.id: true])
but it's not setting anything. If I use setValues
instead, it overwrites the uid
s and stores just the last one, which is not what I want. Here's what I'm trying to do:
// chat_room_members > chatId > userId : true
for user in recipients {
print("user id to be set in chat_room_members:", user.id) // prints successfully all uids of the users
Database.database().reference().child("chat_room_members").child(chatId).updateChildValues([user.id : true])
}
PS: recipients
is an array that stores all the users that the current user wants to start a chat with.
I'm testing this when the chat_room_members
doesn't even exist on the database yet. The very first chat_room
sets this node.
Upvotes: 0
Views: 293
Reputation: 100541
You can try
var dic = [String:Bool]()
recipients.forEach { dic.updateValue(true, forKey:$0.id) }
Database.database().reference().child("chat_room_members").child(chatId).setValue(dic)
Upvotes: 1