Home > Back-end >  How to disable onDisconnect? - Java
How to disable onDisconnect? - Java

Time:11-23

I have a code snippet below that adds a number to the database at a specific path if the user has logged out of the application.

I have a question, how can I make it so that onDisconnect does not fire as I wish? Is it possible?

DatabaseReference presenceRef = FirebaseDatabase.getInstance().getReference("rooms/"   roomName   "/disconnectP1");
presenceRef.onDisconnect().setValue(1);

CodePudding user response:

You can cancel any onDisconnect handler you registered by calling the function that is returned when you call onDisconnect(). From the documentation on how onDisconnect works:

OnDisconnect onDisconnectRef = presenceRef.onDisconnect();
onDisconnectRef.setValue("I disconnected");
// ...
// some time later when we change our minds
// ...
onDisconnectRef.cancel();

When applied to your code, that'd be:

DatabaseReference presenceRef = FirebaseDatabase.getInstance().getReference("rooms/"   roomName   "/disconnectP1");
OnDisconnect onDisconnectRef = presenceRef.onDisconnect();
onDisconnectRef.setValue(1);
...
onDisconnectRef.cancel();
  • Related