Dani
Dani

Reputation: 3637

Firebase UpdateChildValues doesn't set any value

I'm having a chat app where the users could set 1:1 or 1:many chats rooms. I want to store all uids 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 uids 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

Answers (1)

Shehata Gamal
Shehata Gamal

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

Related Questions