如何将数据追加到Firebase中的数组? [英] How to Append data to an Array in Firebase?

查看:116
本文介绍了如何将数据追加到Firebase中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的用户节点中有一个名为 subscribedTo 的数组。现在我想追加一些推式ID的到该数组,每当用户订阅。



但是推入ID 被替换,而不是被追加。



如何将推式ID追加到数组中?

架构

 tester @ gmail,com:{
email:tester @ gmail,com,
hasLoggedInWithPassword:true,
name:tester,
subscribedTo:[
-KFPi5GjCcGrF-oaHnjr
],
timestampJoined:{
timestamp :1459583857967
}
}



CODE

  public void onSubscribe(View v){

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 subscription,Toast.LENGTH_SHORT).show();
}
});

$ b


解决方案

使用地图调用 updateChildren(),Firebase获取每个键,并用该地图中的值替换该位置的对象。



关于 updateChildren()的Firebase文档对此进行了说明:


给定一个像 alanisawesome 这样的单个键路径, updateChildren()只会更新第一个子级别的数据,在第一个子级别之外传入的是一个setValue()操作。


所以在你的情况下, /+ Constants.FIREBASE_LOCATION_USERS +/+ mEncodedEmail +/ subscribedTo的内容。



解决方法是在地图中使 PROG_ID 键的一部分:



pre $ userMap.put(/+ Constants.FIREBASE_LOCATION_USERS +/+ mEncodedEmail +/ subscribedTo /+ PROG_ID,true);
firebaseRef.updateChildren(userMap,...

或者简单地调用<$ c $

  firebaseRef.child( /+ Constants.FIREBASE_LOCATION_USERS +/+ mEncodedEmail +/subscribedTo/\"+PROG_ID).setValue(true); 

你会注意到,在这两种情况下,我摆脱了你的数组,赞成

 subscribedTo:{
-KFPi5GjCcGrF-oaHnjr:true
},


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

    }

解决方案

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
},

这篇关于如何将数据追加到Firebase中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆