0

How to check Fire base query result is 0

I am developing a login page - if the data is not present then directly register the user. But based on the data returned from the fire base , if the result is 0 , then insert into the 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();
            }
        }
    }
});

This is called first [!txt_phone.equals("") && addFlag[0] == false] for a new user, even before the query results is returned. I need it to wait until the result is returned from the firebase, if the firebase returns 0 then this if[!txt_phone.equals("") && addFlag[0] == false] should be executed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Java Carzy
  • 65
  • 1
  • 7

1 Answers1

0

If you are referring to the DataSnapshot you can always call the hasChildren() method which will indicate if there were any query results matching your criteria. That method returns a boolean indicating the result.

B Best
  • 1,106
  • 1
  • 8
  • 26