I have a array called subscribedTo
in my users node. Now I want to append some push ID's
to that array whenever a user is subscribed.
But the push ID
are being replaced instead of getting appended.
How can i append the push ID's to the array?
Schema
"tester@gmail,com": {
"email": "tester@gmail,com",
"hasLoggedInWithPassword": true,
"name": "tester",
"subscribedTo": [
"-KFPi5GjCcGrF-oaHnjr"
],
"timestampJoined": {
"timestamp": 1459583857967
}
}
CODE
public void onSubscribe(View v) {
final Firebase firebaseRef = new Firebase(Constants.FIREBASE_URL);
final HashMap<String, Object> userMap = new HashMap<String, Object>();
pushIDList.add(PROG_ID);
userMap.put("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo",
pushIDList);
firebaseRef.updateChildren(userMap, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
Toast.makeText(ProgramDetail.this, "You are subscribed", Toast.LENGTH_SHORT).show();
}
});
}
This is because Firebase does not support Arrays directly, but it creates a list of objects with integers as key names.
Is there any way to update a specific index from the array in Firestore? No, there is not! This is not possible because if you want to perform an update, you need to know the index of that particular element. When talking about Cloud Firestore arrays, the things are different that you might think.
Inherited from Query.orderByChild. Generates a new Query object ordered by the specified child key. Queries can only order by one key at a time. Calling orderByChild() multiple times on the same query is an error. Firebase queries allow you to order your data by any child key on the fly.27-Jul-2022.
Click on that option and you will get to see two options on Connect app to Firebase and Add Firebase Realtime Database to your app. Click on Connect now and your app will be connected to Firebase. After that click on the second option and now your App is connected to Firebase.
When you call updateChildren()
with a map, Firebase takes each key and replaces the object at that location with the value from the map.
The Firebase documentation on updateChildren()
says this about it:
Given a single key path like
alanisawesome
,updateChildren()
only updates data at the first child level, and any data passed in beyond the first child level is a treated as a setValue() operation.
So in your case, you are replacing the entire contents of "/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo"
.
The solution is to either make the PROG_ID
part of the key in the map:
userMap.put("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID, true);
firebaseRef.updateChildren(userMap, ...
Or to simply call setValue()
at the lower location in the JSON tree:
firebaseRef.child("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID).setValue(true);
You'll note that in both cases I got rid of your array in favor of the recommended structure for such a so-called index:
"subscribedTo": {
"-KFPi5GjCcGrF-oaHnjr": true
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With