Reputation: 15
So I am checking whether the username exists or not during the sign up procedure. The only problem is that my childupdates2 is not being added to the Usernames folder? The first childUpdate is added properly but the second childUpdate2 doesn't. It just doesnt update my Usernames folder?
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);
Here is the database structure.
What am I doing wrong?
Upvotes: 0
Views: 1431
Reputation: 38319
I can't reproduce your results. Add a completion listener to see what the status of the update is:
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());
}
}
});
Also verify that username.toMap()
produces a non-empty map:
Map<String, Object> userdata = username.toMap();
if (userdata.isEmpty()) {
Log.e(TAG, "Oops! No data");
} else {
Log.i(TAG, "userdata=" + userdata);
}
Upvotes: 2