Firebase PresenceManaging iOS

Log*_*gan 5 ios firebase

我正在实现一个基于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];
    }
}];
Run Code Online (Sandbox Code Playgroud)

如果用户完全断开连接,哪个工作正常,但那是我的问题.

我使用这个系统来查看用户是否在线.如果他们不在线,我会触发推送通知.如果用户关闭应用程序,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];
}
Run Code Online (Sandbox Code Playgroud)

这是正常的行为,还是我做错了什么?

Mic*_*uer 4

这是(当前)预期的行为。在客户端实际断开连接之前,Firebase 不会触发状态操作,并且 iOS 会在应用程序进入后台后的一段时间内(可能少于 5 分钟)使底层套接字连接保持活动状态……因此状态将被延迟。但它最终肯定会发生。

您的解决方法应该可以正常工作,或者为了避免破坏整个连接,您可以在进入后台/前台时将存在位设置为@NO / @YES。

我可以看到大多数应用程序如何期望当应用程序进入后台时启动状态,因此我们可能会在未来研究更改此行为。