Adding multiple objects to Firebase Database with one call

1,324 views
Skip to first unread message

Bahadır Yerdelen

unread,
Aug 27, 2017, 10:20:57 PM8/27/17
to Firebase Google Group
Hi,

I am trying to make a chat structure.

-I have a database branch of contacts of user and when I send a message, it adds the messaged user with the value of timestamp to "Contacts" if it is not existing, otherwise updates the timestamp.
-Also I have the messages bransh of the database which saves the sent message under the "Messages" with sent person name and timestamp.

-If I am using setvalueasync method(I am using Unity sdk, it is set value method in other sdk's), I save contacts, message-from-timestamp correctly but then I am sending 4 setvalueasync at once which is inconvenient because this is an operation as a whole and also it is too many calls at once.
-If I am using updatechildren method as a batch, then I can save all but it replaces them and not adding new. Update children does not have Push method to create one.



Below is the code I am using with update children method :

        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
        Dictionary<string, System.Object> update = new Dictionary<string, System.Object>();
        update["/Contacts/" + users[user].name.text] = new Dictionary<string, System.Object>()
        {
            {
                users[(user + 1) % 2].name.text, ServerValue.Timestamp
            }
        };
        update["/Messages/" + (users[0].name.text + "" + users[1].name.text) + "/Message"  ] = new Dictionary<string, System.Object>()
        {
            {
                "From", users[user].name.text
            }
        };
        update["/Messages/" + (users[0].name.text + "" + users[1].name.text) + "/Message"  ] = new Dictionary<string, System.Object>()
        {
            {
                "Message", users[user].message.text
            }
        };
        update["/Messages/" + (users[0].name.text + "" + users[1].name.text) + "/Message"  ] = new Dictionary<string, System.Object>()
        {
            {
                "Time", ServerValue.Timestamp
            }
        };
        reference.UpdateChildrenAsync(update).ContinueWith(task =>
        {
            if (task.Exception != null)
                Debug.Log(task.Exception.ToString());
            else if (task.IsCompleted)
                Debug.Log("Message Sent");
        });;


Is there any way to use batch messaging but also use push to create new one instead of replacing the whole children?

Thanks

 

Kato Richardson

unread,
Aug 28, 2017, 4:04:13 PM8/28/17
to Firebase Google Group

Hello Bahadir,

You can create a push id without actually going and saving a record, and then use that as the key in your dictionary entries. It looks like there’s an example of this under update specific fields, where it calls https://firebase.google.com/docs/database/unity/save-data#update_specific_fields.

I hope that helps!

☼, Kato


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/8ad49b90-7c70-450d-91a4-dcb22f7721fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Kato Richardson

unread,
Aug 28, 2017, 4:05:20 PM8/28/17
to Firebase Google Group

Oops, looks like something went wrong with the markup in that email. In case it's broken for you too, the example looks like this: string key = mDatabase.Child("scores").Push().Key;

Reply all
Reply to author
Forward
0 new messages