Firebase append to objects

Is it possible to just append to Firebase Objects?

When you call a save in Firebase it won’t append to the current value as such, maybe append is the wrong word, but for single values it is okay, but with objects, well I am struggling.

My issue is I am dealing with users who’s accounts contain date like friends (for chats) and all their profile data.

What I want to be able to do is add values to a tag and not have to worry about getting the original value, then uploading the original value plus the new one, as I will be dealing with potentially endless values.

Example:

The main tag is the username. In this tag we have sub tags / objects that contain profile data and friends list. Then another object inside this object called chat list, which contains all the chats sent from that user and the other user. But this chat list object / tag thing can be endless, depending on how many chats that the user has. But I can’t create an object block with 1000 values and hope that works that is just stupid.

Really I need any help I can get in over coming this issue. Is there something I am doing wrong, usually it is.

All help would be much appreciated!

Thank you.

1 Like

The fact is that everything is stored together, under a key. Save again with the same key, and you overwrite the existing one, hence the need to retrieve the whole kit and caboodle before appending and restoring.

But here is something that you could do.

Suppose that you keep the data associated with Bob under the key “Bob1994”.
Under the key “Bob1994”, you do not save the data, you only save the number of sub-records.

So, under Bob1994, you may have the value “3”.
Which means you can expect to also have a record Bob1994#1, Bob1994#2 and Bob1994#3.
If you need to add something, you check the value of Bob1994, notice that it is 3, so the new data would have to be saved under the key Bob1994#4, and when that data is saved, you save 4 under Bob1994 (so next time, you will know that you need to save as Bob1994#5).

If you need to retrieve the data to display, you also need that you should extract Bob1994#1, Bob1994#2, Bob1994#3 and Bob1994#4.

Granted, this is a bit crude, and does not address the possibility that you’d want to erase the sub-record Bob1994#2 while leaving the other intact. You could then save Bob1994#2 with an empty content or with a value that marks it as erased, or have a process that will retrieve record #3 to save as #2, retrieve #4 to replace #3, and erase #4 before saving 3 in Bob1994.

In your specific case, since you have one sub-record that could grow, you could set a limit to the number of chat list object (say 50) before you create another block that could also grow to 50.

Basically, this is how you eat an elephant: you take a lot of bites.

1 Like

this is my current solution as such. the chat id is stored separately and does not look clean, but it works just fine. but would have been great to clean it up.

1 Like