Created
November 16, 2018 10:00
-
-
Save fm-eb/d629a7e236a0066624696c9561aaf8ab to your computer and use it in GitHub Desktop.
Firestore keepalive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private FirebaseDatabase firebaseDatabase; | |
private DatabaseReference connectedRef; | |
private ValueEventListener connectionStateListener; | |
public FirebaseHeartbeatConnector(FirebaseApp firebaseApp) { | |
L.i("FirebaseHeartbeatConnector() called with: firebaseApp = [" + firebaseApp + "]"); | |
firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp); | |
firebaseDatabase.setLogLevel(Logger.Level.DEBUG); | |
} | |
private void doSendFirebaseHeartbeatRequest() { | |
L.i("doSendFirebaseHeartbeatRequest() called"); | |
connectedRef = firebaseDatabase.getReference(); | |
connectedRef.keepSynced(true); | |
connectionStateListener = connectedRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(@NonNull DataSnapshot snapshot) { | |
Boolean connected = snapshot.getValue(Boolean.class); | |
if (connected == null) { | |
changeState(FirebaseConnectionState.error); | |
return; | |
} | |
if (connected) { | |
changeState(FirebaseConnectionState.connected); | |
} else { | |
changeState(FirebaseConnectionState.not_connected); | |
} | |
} | |
@Override | |
public void onCancelled(@NonNull DatabaseError error) { | |
L.i("FirebaseConnectionStateListener was cancelled"); | |
} | |
}); | |
} |
That' really strange. I copy/pasted your code and it worked once. Then I added my firebaseapp-instance as paramter for FirebaseDatabase.getInstance() and it didn't work anymore (1 minute timeout). Then I removed the firebaseapp-instance and it also didn't work anymore...
Then I completely uninstalled the app and went back to your code again => does not work anymore....
i have no idea what's going on there and unfortunately I have no time left to play around with it. So I will implement a normal online-check using the CONNECTIVITY_SERVICE.
Thank you for your time and effort trying to figure this out!! I hope the documentation will improve on this topic.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tested this with the below code and it didn't timeout after 60 seconds for me.