当我尝试插入具有唯一 ID 的新子项时,Firebase 会自动删除前一个子项。这是我的数据库结构:{ "users" : { "EIG2NaC4Y2S5GCbeaKUdpkJgtvp2" : { "email" : "user@gmail.com", "mobile" : "79234792387", "name" : "User", "pets" : { "-LnD1RPwaDILobsUxARh" : { "name" : "PetName", "pet_family" : "Dog" } } } }}这是将新子项插入到 pets 下的方法:public void addPet() { final String petname = petName.getText().toString(); final String petfamily = petFamily.getSelectedItem().toString(); addPetButton.setEnabled(false); final ProgressDialog progressDialog = new ProgressDialog(AddPetActivity.this, R.style.AppTheme_Dark_Dialog); progressDialog.setIndeterminate(true); progressDialog.setMessage("Adding "+petname+"..."); progressDialog.show(); new Handler().postDelayed(new Runnable() { @Override public void run() { FirebaseUser user = mAuth.getCurrentUser(); String userKey = user.getUid(); String petKey = mDatabase.child("users").child(userKey).child("pets").push().getKey(); mDatabase.child("users").child(userKey).child("pets").setValue(petKey); mDatabase.child("users").child(userKey).child("pets").child(petKey).child("name").setValue(petname); mDatabase.child("users").child(userKey).child("pets").child(petKey).child("pet_family").setValue(petfamily); progressDialog.dismiss(); Intent intent = new Intent(getApplicationContext(), NavActivity.class); intent.putExtra("calling-activity","AddPetActivity"); startActivityForResult(intent, 0); } }, 3000);}当我执行代码时,Firebase 创建新的子项,但 2 秒后他删除了另一个。
1 回答
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
错误是:
mDatabase.child("users").child(userKey).child("pets").setValue(petKey);
尝试删除这一行
添加回答
举报
0/150
提交
取消