如何在使用Firebase iOS SDK时检测用户是否在线

mel*_*sam 5 ios firebase

在发送消息(即在Firebase对象上调用setValue)之前,是否有建议的方法来确定用户是在线还是离线?

例如:

[firebase setValue:someValue withCompletionBlock:^(NSError *error, Firebase *ref) {

    // This block is ONLY executed if the write actually completes. But what if the user was offline when this was attempted?
    // It would be nicer if the block is *always* executed, and error tells us if the write failed due to network issues.

}];
Run Code Online (Sandbox Code Playgroud)

我们在iOS应用程序中需要这个,因为如果用户进入隧道,用户可能会失去连接.如果Firebase没有提供内置的方法,我们只需要监控iOS的Reachability API.

Log*_*gan 5

他们在这里有一段专门讨论这个问题的文档.

基本上观察.info/connected参考

Firebase* connectedRef = [[Firebase alloc] initWithUrl:@"https://SampleChat.firebaseIO-demo.com/.info/connected"];
[connectedRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot, NSString *prevName) {
    if([snapshot.value boolValue]) {
        // connection established (or I've reconnected after a loss of connection)
    }
    else {
        // disconnected
    }
}];
Run Code Online (Sandbox Code Playgroud)