gpt4 book ai didi

ios - firebase onDisconnectSetValue 未按预期工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:10:35 26 4
gpt4 key购买 nike

也许我对 firebase 的 onDisconnectSetValue 的理解有误,但我预计会出现以下情况:在 firebase 中,如果应用程序与 firebase 连接,我有一个值为“active”的值。如果连接丢失,我喜欢使用 onDisconnectSetValue 将值设置为 false。为了测试它,我执行以下操作:- 通过互联网连接启动应用程序(设置无线局域网)- 应用程序将“事件”设置为真- 现在我切断互联网连接(关闭 wlan)

现在我希望 firebase 自动将“Active”设置为 false,但该值保持为 true。

奇怪的是,如果我重新连接到互联网(再次设置 wlan),“事件”设置为 false。

代码:

Firebase *userAppActiveRef = [Firebase userAppActiveRef: user.entityID];
Firebase *infoRef = [Firebase infoRef];
[infoRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
if([snapshot.value boolValue]) {
NSLog(@"connected");
[userAppActiveRef setValue: @YES];
[userAppActiveRef onDisconnectSetValue: @NO];
} else {
NSLog(@"not connected");
}
}];

infoRef = .../.info/connected

我做错了什么或者 onDisconnectSetValue 没有按照我想的那样工作?

最佳答案

尝试这个稍微不同的方向(这是您发布的大部分代码的扩展版本)

这有两个部分。第 1 部分是应用知道自己是否已连接(并采取任何一种方式采取行动),第 2 部分是知道其他用户是否已连接:

    //keep track if the app is connected to firebase or not via isConnected 
// isConnected has KVO listeners in the classes so they can take
// action when the user disconnects or reconnects

Firebase *connectedRef = [self.appRef childByAppendingPath:@".info/connected"];

[connectedRef observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

//KVO property will change if the app d/c's
self.isConnected = [snapshot.value boolValue];

if ( self.isConnected ) {
NSLog(@"connected");
[thisUserStatusRef setValue:@"YES"];

} else {
NSLog(@"d/c'd!! Run for the hills!");
}

}];

通过此设置,应用知道何时连接并将 thisUsersStatusRef 设置为 YES。

然后,设置onDisconnect在用户断开连接时采取行动

[thisUserStatusRef onDisconnectRemoveValue];

这告诉服务器在该客户端断开连接时删除 thisUsersStatusRef(您也可以设置为 NO)。

因此当用户连接时,thisUsersStatusRef 被设置为 YES,当它断开连接时,该值被删除。

最后,让您的应用程序观察用户节点是否有任何更改 - 如果其他用户连接到应用程序,将收到通知;如果他们断开连接,他们也会收到通知。

[usersRef observeEventType:FEventTypeChildChanged withBlock:^(FDataSnapshot *snapshot) {

//the snapshot will contain the user that connected or disconnects
// so just test to see if status is YES or null

}];

关于ios - firebase onDisconnectSetValue 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com