I have the following Firebase database structure. uIds - type List<String> . I am trying to add another uId under uIds with incremental index. setValue() and updateChildren() will require me to get the existing data, and push() will add an element with a randomly generated string as a key instead of the index with the addition. Is there an easier way that does not require existing data to be extracted? Thanks!
"requests" : { "request001" : { "interests" : [ "x" ], "live" : true, "uIds" : [ "user1" ] // <---- from this }, "request002" : { "interests" : [ "y" ], "live" : true, "uIds" : [ "user2" ] } }
--------------------------------
Edit:
Sorry for the aversion. Let me make it clear. Let's say I have the above database and you want to update it to the next.
"requests" : { "-KSVYZwUQPfyosiyRVdr" : { "interests" : [ "x" ], "live" : true, "uIds" : [ "user1", "user2" ] // <--- to this }, "-KSl1L60g0tW5voyv0VU" : { "interests" : [ "y" ], "live" : true, "uIds" : [ "user2" ] } }
the ishmaelMakitla clause, mDatabase.child("requests").child("request001").setValue(newRequest) , will overwrite "request001" with "newRequest". So I have to get the existing data "request001" and add "user2" to the uIds list. It will be something like this:
mDatabase.child("requests").child("request001").addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Request newRequest = dataSnapshot.getValue(Request.class); newRequest.uIds.add("user2"); mDatabase.child("requests").child("request001").setValue(newRequest); } @Override public void onCancelled(DatabaseError databaseError) {} });
But I am wondering if this process is needed as I am trying to just add one item to the uIds list.
source share