简体   繁体   English

在iOS上与Firebase断开连接

[英]Disconnecting from Firebase on iOS

I have a onDisconnectRemoveValue on a database reference to show when a user is currently 'in chat' and I would like to disconnect from the FIRDatabase when the user leaves the chat view controller - how can I trigger this disconnect function without quitting the app completely? 我在数据库引用上有一个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];
            }];
        }
    }];

You can call [[FIRDatabase database] goOffline]; 您可以致电[[FIRDatabase database] goOffline]; to immediately trigger the connection to be closed. 立即触发连接被关闭。 This will trigger the onDisconnect handler on the server, although there may be a delay before that runs. 这将触发服务器上的onDisconnect处理程序,尽管在运行之前可能会有延迟。

You have to disconnect and reconnect the firebase connection with remove all observers between them. 您必须断开并重新连接Firebase连接,并删除它们之间的所有观察者。

Disconnect fire the onDisconnectRemoveValue after disconnect you have to reconnect to keep your firebase alive in order to continue work with other conversations but before reconnect you have to remove all observers to make sure that connectedRef don't fire again in reconnect and re add your values again. 断开连接后,请断开连接,以重新启动onDisconnectRemoveValue ,以使Firebase保持活动状态,以便继续进行其他对话,但是在重新连接之前,必须删除所有观察者,以确保connectedRef在重新连接时不会再次触发并再次添加值。

[[FIRDatabase database] goOffline];

if (_connectedRef) {
    [_connectedRef removeAllObservers];
    _connectedRef = nil;
}

[[FIRDatabase database] goOnline];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM