Halpo Halpo - 1 year ago 109
iOS Question

Disconnecting from Firebase on iOS

I have a

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?

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];

Answer Source

You can call [[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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download