updateChildren没有更新孩子?

时间:2016-08-31 09:43:08

标签: android firebase firebase-realtime-database

所以我在注册过程中检查用户名是否存在。唯一的问题是我的 childupdates2 没有被添加到用户名文件夹中?第一个 childUpdate 已正确添加,但第二个 childUpdate2 未正确添加。它只是不更新​​我的用户名文件夹?

 final DatabaseReference Userref = FirebaseDatabase.getInstance().getReference();
    username_check_query = Userref.child("Usernames").orderByChild("username").equalTo(mUsernameView.getText().toString());
    usernameChildEvent = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.getValue() == null){

                ScoreClass scoreClass = new ScoreClass(10000, mUsernameView.getText().toString());
                Map<String, Object> scoredata = scoreClass.toMap();
                Map<String, Object> childUpdates = new HashMap<>();
                childUpdates.put("Users/" + user.getUid(), scoredata);
                Userref.updateChildren(childUpdates);


                Username username = new Username(mUsernameView.getText().toString());
                Map<String, Object> userdata = username.toMap();
                Map<String, Object> childUpdates2 = new HashMap<>();
                childUpdates2.put("/Usernames/" + user.getUid(), userdata);
                Userref.updateChildren(childUpdates2);
           }
           else {
                    // other code
            }
            username_check_query.removeEventListener(this);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
username_check_query.addListenerForSingleValueEvent(usernameChildEvent);

这是数据库结构。

database structure

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我无法重现您的结果。添加完成侦听器以查看更新的状态:

    Userref.updateChildren(childUpdates2, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                    if (databaseError == null) {
                        Log.i(TAG, "onComplete: success");
                    } else {
                        Log.w(TAG, "onComplete: fail", databaseError.toException());
                    }
                }
            });

同时验证username.toMap()是否生成非空地图:

    Map<String, Object> userdata = username.toMap();
    if (userdata.isEmpty()) {
        Log.e(TAG, "Oops! No data");
    } else {
        Log.i(TAG, "userdata=" + userdata);
    }