11

I'm trying to make a read query timeout with Firebase Database when there is no internet connection or any other problem. The problem is that even when the timeout takes effect and the listener is removed, it still gets called even though it has been removed. How can I prevent that? What's the best way to handle timeout?

Here's my code :

final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("key");
final ValueEventListener listener = new ValueEventListener() { /* ... */ };
ref.addListenerForSingleValueEvent(listener);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        ref.removeEventListener(listener);
    }
}, 5000);

Someone here seems to have the same issue but I don't understand the solution.

3
  • Did you get the solution yet? Sep 23, 2017 at 10:23
  • @ParagKadam No, I didn't look much into it
    – Nicolas
    Sep 25, 2017 at 23:01
  • I have solved it, check my answer out. Sep 26, 2017 at 9:21

1 Answer 1

11

There is nothing like timeout in the Firebase API, hence timeouts should be handled using our own logic. This is how i implement timeouts in my code.

    private void getDataFromFirebase()
    {
        final boolean[] gotResult = new boolean[1];
        gotResult[0] = false;

        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference dataReference = firebaseDatabase.getReference().child("data");

        ValueEventListener dataFetchEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                gotResult[0] = true;
                // You async code goes here
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                gotResult[0] = true;
            }
        };

        if(isNetworkAvailable()) {

            dataReference.addListenerForSingleValueEvent(dataFetchEventListener);
            Timer timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    timer.cancel();
                    if (gotResult[0] == false) { //  Timeout
                        dataReference.removeEventListener(dataFetchEventListener);
                        // Your timeout code goes here
                    }
                }
            };
            // Setting timeout of 10 sec to the request
            timer.schedule(timerTask, 10000L);
        }
        else{
            // Internet not available
        }
    }
1
  • 2
    You could've used AtomicBoolean: AtomicBoolean gotResult = new AtomicBoolean(false) to set use gotResult.set(true) and to retrieve use gotResult.get() which returns a primitive boolean Jul 2, 2018 at 22:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.