dmreshet dmreshet - 1 month ago 15x
Android Question

Firebase update list values

There is datastructure:

0 : category1,
1 : category2,
2 : category3

I want to concurrently add values to this structure.
It looks like updateChildren() method will replace list with a new one.

Map<String, Object> childUpdates = new HashMap<>();

What is the best practise to implement this?


That's bad practice. (To find out why, look here)and . Instead of using 0:,1:, etc, use the Firebase push() method. Which generates a unique key like -K9929EDDJDODAK939. Your structure will look like this:

   -K9929EFDADFAK939: category 1,
   -DFADF9ADFFDF: category 2,
    K99DF9EDDJDO9: category 3

To this, use the push() method when saving those into the database, like this:


You don't need to use updateChildren(), just save them like I mentioned. It will keep on saving and won't replace ANY existing.

Fore more information, you should read the docs, and especially how to retrieve data when using this method.

For more information on avoiding Arrays in Firebase: (Thanks for @Wiliks for the link) Best Practices: Arrays in Firebase