如何检查Fire基础查询结果是否为0
我正在开发一个登录页面-如果数据不存在,那么直接注册用户。但根据fire base返回的数据,如果结果为0,则插入fire base
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]
应该被执行。
发布于 2019-05-31 03:35:03
如果您指的是DataSnapshot
,那么您可以随时调用hasChildren()
方法,该方法将指示是否有任何查询结果与您的条件匹配。该方法返回一个指示结果的布尔值。
https://stackoverflow.com/questions/56383472
复制相似问题