我是Firebase的新手,需要知道如何检查我的写作任务是否成功,因为如果不这样做,MainActivity会启动并且会影响我的注册进度.
这将检查是否已采用Username,如果不是,则注册用户:
Query usernamequery = myRef.orderByChild("Username").equalTo(Username); usernamequery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { if (snapshot.exists()) { // TODO: handle the case where the data already exists editText.setError("Username taken!"); return; } else { // TODO: handle the case where the data does not yet exist myRef.child("Users").child(Username).child("Userid").setValue(user.getUid()); myRef.child("Users").child(Username).child("Username").setValue(Username); startActivity(maps); finish(); } } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(Username.this,"Error",Toast.LENGTH_LONG).show(); } });
但我希望只有在激活时才会触发Intent to Main Activity(地图)
.myRef.child( “用户”)孩子(用户名).child( “用户ID”)的setValue(user.getUid()).
另一个完成任务并取得成功.
我能做什么?
解决方法@H_301_15@
要知道写操作何时完成,请添加完成侦听器:
myRef.child("Users").child(Username).child("Userid").setValue(user.getUid(),new DatabaseReference.CompletionListener() {
void onComplete(DatabaseError error,DatabaseReference ref) {
System.out.println("Value was set. Error = "+error);
}
});
如果要通过单个操作写入多个位置,则需要查看update()方法:https://firebase.google.com/docs/database/android/read-and-write#updating_or_deleting_data
myRef.child("Users").child(Username).child("Userid").setValue(user.getUid(),new DatabaseReference.CompletionListener() { void onComplete(DatabaseError error,DatabaseReference ref) { System.out.println("Value was set. Error = "+error); } });
如果要通过单个操作写入多个位置,则需要查看update()方法:https://firebase.google.com/docs/database/android/read-and-write#updating_or_deleting_data