Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

android - Firebase: when onDisconnect event fire?

I`m using Firebase-backend for my android applicaion. I'd like to build a users presence system for my chat. For this purpose I've taken the pattern from Firebase Guide

final Firebase myConnectionsRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/connections");

// stores the timestamp of my last disconnect (the last time I was seen online)
final Firebase lastOnlineRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/users/joe/lastOnline");

final Firebase connectedRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/.info/connected");
connectedRef.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    boolean connected = snapshot.getValue(Boolean.class);
    if (connected) {
      // add this device to my connections list
      // this value could contain info about the device or a timestamp too
      Firebase con = myConnectionsRef.push();
      con.setValue(Boolean.TRUE);

      // when this device disconnects, remove it
      con.onDisconnect().removeValue();

      // when I disconnect, update the last time I was seen online
      lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);
    }
  }

  @Override
  public void onCancelled(FirebaseError error) {
    System.err.println("Listener was cancelled at .info/connected");
  }
});

The trouble is I don't know when onDisconnect event would be fired?!

Every time I open the app I write TRUE to the node "users/joe/connections" but when I closed app nothing would be happend. When I switched WIFI off then the boolean parameter wouldn't be removed as well. onDisconnect-event only fired when I forcely stopped my app or reinstalled this app or somewhen else when I couldn`t determine.

So, as I understand right I have to handle manually such events:

1) close my app;

2) switch WiFi off;

3) maybe something else

for creating Presence feature in my app? But then when have onDisconnect-event to be fired ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...