如何检查Fire base查询结果是否为
我正在开发一个登录页面-如果不存在数据,则直接注册用户。但是根据火力基地返回的数据,如果结果为0,则将其插入火力基地
if (!txt_phone.equals("")) {
Query query = auth.orderByChild("phoneNo").equalTo(StringUtils.trim(txt_phone)).limitToFirst(1);
query.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User exisitngUser = null;
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
exisitngUser = snapshot.getValue(User.class);
}
Log.d(TAG, "Exisiting User" + exisitngUser.toString());
if (exisitngUser != null && exisitngUser.getPhoneNo() != null && exisitngUser.getPhoneNo().equalsIgnoreCase(txt_phone)) {
Log.i(TAG, "onDataChange Method");
addFlag[0] = true;
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("phoneNo", txt_phone);
intent.putExtra("userName", txt_userName);
startActivity(intent);
finish();
} else if (addFlag[0] = false) {
Log.i(TAG, "onDataChange Method else");
addFlag[0] = true;
addUser = FirebaseDatabase.getInstance().getReference("Messanger");
User login = new User("1", txt_userName, txt_password, txt_phone, null, "Active", null);
addUser.child("loginUser").push().setValue(login);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("phoneNo", txt_phone);
intent.putExtra("userName", txt_userName);
startActivity(intent);
finish();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
//This is called before the data is returned, How to I block this until the data is checked
if (!txt_phone.equals("") && addFlag[0] == false) {
Log.i(TAG, "Out of onDataChange Method");
addFlag[0] = true;
addUser = FirebaseDatabase.getInstance().getReference("Message");
User user = new User("1", txt_userName, txt_password, txt_phone, null, "Active", null);
addUser.child("User").push().setValue(user);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("phoneNo", txt_phone);
intent.putExtra("userName", txt_userName);
startActivity(intent);
finish();
}
}
}
});
即使在返回查询结果之前,也将其称为新用户的第一个[!txt_phone.equals("") && addFlag[0] == false]
。我需要它等待直到从Firebase返回结果,如果Firebase返回0,则应执行此if[!txt_phone.equals("") && addFlag[0] == false]
。
答案 0 :(得分:0)
如果您指的是DataSnapshot
,则可以随时调用hasChildren()
方法,该方法将指示是否有符合您条件的查询结果。该方法返回一个指示结果的布尔值。