Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Append data to an Array in Firebase?

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();
            }
        });

    }
like image 250
user3467240 Avatar asked Apr 16 '16 03:04

user3467240


People also ask

Can we store array in Firebase?

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?

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.

What is orderByChild in Firebase?

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.

How do I add and retrieve data from Firebase?

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.


1 Answers

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
},
like image 156
Frank van Puffelen Avatar answered Sep 28 '22 10:09

Frank van Puffelen