【问题标题】:Disconnecting from Firebase on iOS在 iOS 上断开与 Firebase 的连接
【发布时间】:2019-03-02 00:37:21
【问题描述】:

我在数据库引用上有一个onDisconnectRemoveValue,用于显示用户当前“正在聊天”的时间,并且我想在用户离开聊天视图控制器时断开与 FIRDatabase 的连接 - 我如何在没有完全退出应用?

FIRDatabaseReference *connectedRef = [self.dbRef child:@".info/connected"];
    [connectedRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
        if (snapshot.value) {
            NSString *path = [NSString stringWithFormat:@"connections/%@/participants",self.refID];
            FIRDatabaseReference *participantsRef = [self.dbRef child:path];
            [participantsRef setValue:@{@"avatar":@"avatarURL",@"handle":[self senderDisplayName]} withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
                [ref onDisconnectRemoveValue];
            }];
        }
    }];

【问题讨论】:

    标签: ios objective-c firebase firebase-realtime-database


    【解决方案1】:

    您可以调用[[FIRDatabase database] goOffline]; 立即触发关闭连接。这将触发服务器上的 onDisconnect 处理程序,尽管在运行之前可能会有延迟。

    【讨论】:

    • 这是 JS - 在 iOS SDK 中看不到这个
    • 我提供的链接适用于 iOS。
    • 当我使用 goOffline 后又重新上线时,每次我重新连接时都会触发多次“.info/connected”侦听器?
    【解决方案2】:

    您必须断开并重新连接 firebase 连接,并删除它们之间的所有观察者。

    Disconnect 在断开连接后触发 onDisconnectRemoveValue,您必须重新连接以保持您的 firebase 处于活动状态才能继续处理其他对话,但在重新连接之前,您必须删除所有观察者以确保 connectedRef 不要在重新连接时再次触发并重新添加您的值。

    [[FIRDatabase database] goOffline];
    
    if (_connectedRef) {
        [_connectedRef removeAllObservers];
        _connectedRef = nil;
    }
    
    [[FIRDatabase database] goOnline];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多