0

I want to check if the specific user is present in database or not and if it is present then I don't want to override the data in the database. I am using unique key generation at every node provided by firebase as User Id's

So I don't want to duplicate the user if he is already present.

How can I check if this user is present in database without listening to any events because my program execution is dependent on this values

When I am doing this the execution goes in a infinite loop

    USER_REFERENCE.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Iterable<DataSnapshot> children = dataSnapshot.getChildren();
                for (DataSnapshot d : children) {
                    if (d.child("email").exists()) {
                        if (d.child("email").getValue().equals(user.getEmail())) {
                            user.setUserId(d.getKey());
                            mPresenter.saveUserIntoSession(user);
                        } else {
                            String userId = addUser(user);
                            user.setUserId(userId);
                            mPresenter.saveUserIntoSession(user);
                        }
                    }
                }
            }
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
kemblekaran
  • 174
  • 2
  • 14

3 Answers3

1

Try this to check:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users");
ref.orderByChild("username").equalTo(name).addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
  if(dataSnapshot.exist() {
     //username exist
      }
    }

The above will read data once and it will check if a particular username exists in the database

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
1

Use addListenerForSingleValueEvent to read data once.

ArneHugo
  • 6,051
  • 1
  • 26
  • 47
  • How does this differ from Peter Haddad's answer expected you added a link to the documentation? – J. Doe Apr 03 '18 at 11:37
  • He uses the same solution, but shows it with an example, while I explicitly state which method to use and link to the docs. The ideal answer should do both. Not sure if I should edit my answer or his. – ArneHugo Apr 03 '18 at 12:35
0

Restating the answer in simpler terms:

Given:

@Override public boolean onLongClick(View view) {

return true; // or false

} return true means that the event is consumed. It is handled. No other click events will be notified. return false means the event is not consumed. Any other click events will continue to receive notifications. So if you don't want onClick to also be triggered after an onLongClick, then you should return true from the onLongClick event.

husen
  • 180
  • 1
  • 2
  • 11