我正在实现一个基于firebase文档的系统:
[connectionMonitor observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) { if([snapshot.value boolValue]) { // connection established (or I've reconnected after a loss of connection) // add this device to my connections list // this value could contain info about the device or a timestamp instead of just true Firebase * con = [[Firebase alloc]initWithUrl:[NSString stringWithFormat:@"%@Users/%@/connections/", urlString, currentUserId]]; Firebase * newConnection = [con childByAutoId]; [newConnection setValue:@YES]; // when this device disconnects, remove it [newConnection onDisconnectRemoveValue]; } }];
如果用户完全断开连接,哪个工作正常,但那是我的问题.
我使用这个系统来查看用户是否在线.如果他们不在线,我会触发推送通知.如果用户关闭应用程序,firebase不会断开连接,但它也不会收到更新,因此在另一端,用户看起来仍然在线.要正确设置firebase onDisconnect值,用户需要完全关闭应用程序.
我通过添加以下内容解决了这个
- (void)applicationWillResignActive:(UIApplication *)application { [Firebase goOffline]; } - (void)applicationDidEnterBackground:(UIApplication *)application { [Firebase goOffline]; } - (void)applicationWillEnterForeground:(UIApplication *)application { [Firebase goOnline]; } - (void)applicationDidBecomeActive:(UIApplication *)application { [Firebase goOnline]; }
这是正常的行为,还是我做错了什么?