【发布时间】:2019-08-07 13:36:26
【问题描述】:
我希望允许用户在不覆盖存储在其 UID 下的信息的情况下发布大量帖子我知道随机生成密钥的推送方法,但我宁愿不使用它。一些帮助将不胜感激。在下面的方法中,我基本上将包含用户信息的字典保存到Firebase 实时数据库中。但是由于设置值方法,信息正在被替换。
数据库:
Post
NEbIdbCumcOnIbPdUQxpwtV7Dr63 (UID)
desc: "Toue moe"
id: "NEbIdbCumcOnIbPdUQxpwtV7Dr63"
image: "Contains some image"
name: "Some name"
profileimage: "Image"
代码:
public void saveToFirebase() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("Uploading...");
progressDialog.show();
//final String userId = mAuth.getCurrentUser().getUid();
final StorageReference storageRef = FirebaseStorage.getInstance().getReference().child("images").child(userId);
storageRef.putFile(selectedImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
url = uri.toString();
postDictionary.put("desc", descriptionEditText.getText().toString());
postDictionary.put("image", url);
postDictionary.put("id", userId);
postDictionary.put("name",name);
postDictionary.put("profileimage", profileImage);
productsDatabaseRef.child("Post").child(userId).setValue(postDictionary);
progressDialog.dismiss();
Intent intent = new Intent(Upload_Post.this, Shop_Activity.class);
startActivity(intent);
finish();
}
//handles error
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(Upload_Post.this, "Error" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
【问题讨论】:
-
嘿阿伦,您可以使用 firebase updateChildren() 方法进行数据库参考
-
嗨,我试过这样使用,但当前信息仍在被替换 productsDatabaseRef.child("Post").child(userId).updateChildren(postDictionary);
-
显示你的 firebase 数据库节点
-
我编辑了我的问题,请在上面查看。这就是它的结构。我基本上是在尝试让用户发布大量帖子并将信息保存在他们的 UID 下。
-
嗯,我看到在您的 postDictionary 地图中,您包含了数据库节点中的所有内容。这就是为什么所有人都在替换
标签: android firebase firebase-realtime-database