So I'm building presence into my app by setting a flag in firebase called `isOnline` to true when the app becomes active and when I initialize the account. For the most part this works, the user is flagged as offline when I kill the app. But when I go into airplane mode, my disconnect handler is never called and the user still appears online.
After reestablishing the connection, I do an action that writes to firebase, and on this write (could be anything at all), the `isOnline` flag is usually set to false. There were a couple occasions where it sets to false, then immediately to true. I'm a bit confused.
This is how I'm handling going online:
- (void)goOnline
{
Firebase *connectedref = [[[Firebase alloc] initWithUrl:kPULFirebaseURL] childByAppendingPath:@".info/connected"];
[connectedref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
if ([snapshot.value boolValue])
{
Firebase *fire = [[[[[Firebase alloc] initWithUrl:kPULFirebaseURL] childByAppendingPath:@"users"] childByAppendingPath:[PULAccount currentUser].uid] childByAppendingPath:@"isOnline"];
[fire setValue:@(YES)];
[fire onDisconnectSetValue:@(NO)];
}
}];
}
I call this method in my app delegate whenever the app becomes active and on launch. I am writing data when the app is in the background, so it only disconnects from firebase when the app is terminated or when data is not written in the background for ten to twenty minutes.
What might cause this odd behavior? And is it ok to be setting the disconnect handler when it might already exist?