Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop value event listener from listening after a condition is met

for example searching in circles for a user circle if a data exist I want it DO process 1 and if data doesn't exist I want it to create one then DO process 2 ... but what happens in this code if data doesn't exist it will create one then do process 2 then go back then do process 1 after checking. so how can I stop the listener after process 2. sorry if the circles example is too ambiguous but this is the simplest example I could think of .

Firebase ref= new Firebase("https://XXXXX.firebaseio.com/circles/");
Query queryRef = ref.orderByChild("circalename").equalTo(user.circalename);
ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot datasnapshot) {
        if (datasnapshot.exists()) { // I don't want to get back here after 
                                      //creating the data in the else statement 
             // DO process 1
         }

         // if my data doesnt exist I will create one after that STOP listening 
         else {
             // create circle 
             // do process 2
         }

    }
like image 804
toix12345 Avatar asked Apr 04 '16 20:04

toix12345


2 Answers

What you can do is use addListenerForSingleValueEvent

as it listens to the event only once

i.e. in your case

queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        if (datasnapshot.exists()) {
            // code if data exists
        } else {
            // code if data does not  exists
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
like image 150
Shubham Arora Avatar answered Oct 19 '22 23:10

Shubham Arora


Despite Shubham Arora's answer is the best solution for this case, I'm going to show you how to do exactly what you asked with these two solutions that are quite simple:


1. Global boolean

Create a global boolean and change it once your condition is met:
query.addListenerForSingleValueEvent(new ValueEventListener() {
    boolean processDone = false;

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists() && !processDone) {
            // do process 1   
        } else {
            // do process 2
            processDone = true;
        }
    }

    @Override public void onCancelled(DatabaseError databaseError) {}
});


2. Remove listener

Remove your listener once your condition is met:
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            // do process 1   
        } else {
            // do process 2
            query.removeEventListener(this);
        }
    }

    @Override public void onCancelled(DatabaseError databaseError) {}
});


Both solutions did work fine for me when I was using ChildEventListener and Shubham Arora's answer couldn't help.

like image 33
Hugo Passos Avatar answered Oct 19 '22 22:10

Hugo Passos