【问题标题】:Updating value in firebase removes children更新firebase中的值会删除孩子
【发布时间】:2018-04-10 09:27:54
【问题描述】:

我的应用有一个用户个人资料和一个愿望清单,它们都保存在 firebase 上

[使用的火力树]

问题是当用户更改他/她的国家、性别或姓名时,整个 WishList 孩子会被自动删除我仍然不知道为什么,所以你们能帮忙吗? 这些是我的代码的 sn-ps:

Profile Activity 中更新 proflie 的方法:

public void updateProfile() {
    String name = editName.getText().toString().trim();
    String country = countriesSpinner.getSelectedItem().toString();
    String gender = genderSpinner.getSelectedItem().toString();

    UserInfo userInfo = new UserInfo(name, country, gender);
    databaseReference.child(user.getUid()).setValue(userInfo);
}

在其他活动中将商品添加到愿望清单(产品信息存储在本地数据库中):

 Button wishListBtn = (Button) findViewById(R.id.wishListBtn);
    wishListBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(firebaseAuth.getCurrentUser() != null) {
                final FirebaseUser user = firebaseAuth.getCurrentUser();
                final String key = databaseReference.child(user.getUid()).child("WishList").push().getKey();

                final WishlistItem wlItem = new WishlistItem();
                wlItem.setwId(key);
                wlItem.setwName(cursor.getString(1));
                wlItem.setwStore(cursor.getString(2).toUpperCase());
                wlItem.setwCategory(cursor.getString(3));




                databaseReference.child(user.getUid()).child("WishList").orderByChild("wName").equalTo(cursor.getString(1)).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if( dataSnapshot.exists()) {
                            //Value exists
                            Toast.makeText(ProductDetails.this, "Already added to Wish List", Toast.LENGTH_SHORT).show();

                        } else {
                            //Value doesn't exist
                            databaseReference.child(user.getUid()).child("WishList").child(key).setValue(wlItem);
                            Toast.makeText(ProductDetails.this, "Product Added successfully to Wish List", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    通过使用

    databaseReference.child(user.getUid())
                     .setValue(userInfo);
    

    你覆盖整个节点。

    你可以做(​​例如)

    databaseReference.child(user.getUid())
                     .child("uCountry")
                     .setValue(country)
    

    或者您将另一个孩子添加到 userId 中,例如 userInfo,其中包含国家、性别、姓名和行为

    databaseReference.child(user.getUid())
                     .child("userInfo")
                     .setValue(userInfo);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2018-09-17
      • 2019-01-21
      • 2018-08-19
      • 2018-11-24
      相关资源
      最近更新 更多