简体   繁体   中英

Does “removeEventListener” (android) close Firebase Realtime Database connection?

I went through all the docs and so many blogs but did not get an answer.

Thing is that I have an initial free quota of 100 simultaneous connections to the firebase realtime database.That means at a time 100 of my users can use my application.After using the application unless and until a user does not destroy the application the connection still remains open for that user so at the meantime no other user can get access.

So my question is that if I remove the listener from the Database Reference will this close the connection as well?

 ValueEventListener listener = null;
 DatabaseRefference ref =FirebaseDatabase.getInstance().getRefference("user");
 listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ref.removeEventListener(listener); 
        //----Doing Stuffs
    }
 }
ref.addListenerForSingleValueEvent(listener);

With this what I want to achieve is that when User1 gets his search result he/she will no longer hold a connection and in this way another user can get into the database.

Firebase automatically manages the connection. This is explained in the Realtime Database Guide :

On Android, Firebase automatically manages connection state to reduce bandwidth and battery usage. When a client has no active listeners, no pending write or onDisconnect operations, and is not explicitly disconnected by the goOffline method, Firebase closes the connection after 60 seconds of inactivity.

Also note that when a listener is added with addListenerForSingleValueEvent() it is automatically removed after onDataChange() runs. You don't need to call removeEventListenter() , as you have done in the posted code. This is described in the documentation :

Read data once

In some cases you may want a callback to be called once and then immediately removed, such as when initializing a UI element that you don't expect to change. You can use the addListenerForSingleValueEvent() method to simplify this scenario: it triggers once and then does not trigger again.

You can detect connection state changes with a listener at pseudo-location /.info/connected . Note that this does count as a normal, "active" listener and will not inhibit the auto-disconnect processing.

Another way to get visiblilty on the connection state management is to enable debug logging with FirebaseDatabase.getInstance().setLogLevel(Logger.Level.DEBUG) .

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