3

I need to add data to an array stored in the database to be like this: places:

0:"new Item"

1:"new Item",

2:"new Item"

.

.

.

My problem is How to push data in array stored in the Firebase without the need to use a Hashmap? For exemple the next data pushed will result on:

places: 0:"old Item"

1:"vItem",

2:"old Item",

3:"new Item"

I know that if i use the method given bellow, the data will be erased and a new data set will be added,

Utils.eventsRef.child(events.getUid()).child("arrayTicket").setValue(str);

Can someone help me please ?

5
  • Have you solved the issue? Commented May 6, 2019 at 11:10
  • no not yet, i left it, i just changed the database structure. Please if you find an answer can you share it with me ? Commented May 6, 2019 at 11:24
  • So to understand better, you have an array that contains some string elemenets and you want to add that array to the Firebase database as 0:"new Item" 1:"new Item", 2:"new Item", right? Commented May 6, 2019 at 11:26
  • yes it's exactly what i need . Commented May 7, 2019 at 8:04
  • Ok, I'll write you an answer right away. Commented May 7, 2019 at 9:39

1 Answer 1

2

According to your comments, to be able to add an array into your Firebase realtime database, you need first to convert your array to a List like in the following lines of code:

String[] items = {"newItem", "newItem", "newItem"};
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference arrayTicketRef = rootRef.child("arrayTicket");
arrayTicketRef.setValue(Arrays.asList(items));

The result in your Firebase console will be:

Firebase-root
   |
   --- arrayTicket
         |
         --- 0: "newItem"
         |
         --- 1: "newItem"
         |
         --- 2: "newItem"
Sign up to request clarification or add additional context in comments.

5 Comments

ok, with that you can initialize your array, but how can you push a new value inside arrayTicket without erasing the old values ?
In this case, you will have to read the existing data (arrayTicket object), then write it back with the new value added. Arrays like this are not always the best way to store lists of data if you want to perform a lot of append operations. For that, you're better off pushing data into a location using push() method.
i am looking for a way where i can push without reading the existing data. My constraint her is if 2 devices read the same data at the same time and push at the same time, one of them will erase the other user data, that's why i am looking for a way to push without reading the existing data.
There is no way you can achieve this in Firebase realtime database. However, this is actually possible in Cloud Firestore where you can add/remove items from an array without the need to read the existing data and then write it back. Is it ok now?
if you think that my question might be useful and clear, please consider to give a vote-up(▲). I'd appreciate it.Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.