如何将数据附加到 Firebase 中的数组?
How to Append data to an Array in Firebase?
我的用户节点中有一个名为 subscribedTo
的数组。现在我想在用户订阅时向该数组附加一些 push ID's
。
但是 push ID
被替换而不是附加。
如何将推送 ID 附加到数组?
架构
"tester@gmail,com": {
"email": "tester@gmail,com",
"hasLoggedInWithPassword": true,
"name": "tester",
"subscribedTo": [
"-KFPi5GjCcGrF-oaHnjr"
],
"timestampJoined": {
"timestamp": 1459583857967
}
}
代码
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();
}
});
}
当您使用地图调用 updateChildren()
时,Firebase 获取每个键并将该位置的对象替换为地图中的值。
updateChildren()
上的 Firebase 文档是这样说的:
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.
因此,在您的情况下,您要替换 "/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo"
的全部内容。
解决方案是将PROG_ID
部分映射到键中:
userMap.put("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID, true);
firebaseRef.updateChildren(userMap, ...
或者在 JSON 树的较低位置简单地调用 setValue()
:
firebaseRef.child("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID).setValue(true);
你会注意到,在这两种情况下,我都去掉了你的数组,取而代之的是 recommended structure for such a so-called index:
"subscribedTo": {
"-KFPi5GjCcGrF-oaHnjr": true
},
我的用户节点中有一个名为 subscribedTo
的数组。现在我想在用户订阅时向该数组附加一些 push ID's
。
但是 push ID
被替换而不是附加。
如何将推送 ID 附加到数组?
架构
"tester@gmail,com": {
"email": "tester@gmail,com",
"hasLoggedInWithPassword": true,
"name": "tester",
"subscribedTo": [
"-KFPi5GjCcGrF-oaHnjr"
],
"timestampJoined": {
"timestamp": 1459583857967
}
}
代码
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();
}
});
}
当您使用地图调用 updateChildren()
时,Firebase 获取每个键并将该位置的对象替换为地图中的值。
updateChildren()
上的 Firebase 文档是这样说的:
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.
因此,在您的情况下,您要替换 "/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo"
的全部内容。
解决方案是将PROG_ID
部分映射到键中:
userMap.put("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID, true);
firebaseRef.updateChildren(userMap, ...
或者在 JSON 树的较低位置简单地调用 setValue()
:
firebaseRef.child("/" + Constants.FIREBASE_LOCATION_USERS + "/" + mEncodedEmail + "/subscribedTo/"+PROG_ID).setValue(true);
你会注意到,在这两种情况下,我都去掉了你的数组,取而代之的是 recommended structure for such a so-called index:
"subscribedTo": {
"-KFPi5GjCcGrF-oaHnjr": true
},