简体   繁体   中英

How to get all active real-time listeners in Firebase Android

I have a use case where I need to know the currently active realtime listeners in the client. This is so that I can remove the listeners from anywhere in the code. I attached the listener in activity A and would need to remove it in activity B when an event occurs. Is there a way I can achieve this?

I have a use case where I need to know the currently active realtime listeners in the client. This is so that I can remove the listeners from anywhere in the code.

In my opinion, there is no need to know how many listeners are currently active, as long as you remove them according to the life-cycle of your activity .

I attached the listener in activity A and would need to remove it in activity B when an event occurs.

You should always remove the listener once you leave activity B. If you need to take some action when an event occurs in activity B, then you should add another listener, which is different than the listener in activity A.

Please also note that Firestore can clean up the all listeners automatically when the activity is stopped if you are passing as the fist argument your activity:

ListenerRegistration registration = docRef.addSnapshotListener(ActivityA.this, listener);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM