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.