Before sending a message (i.e. calling setValue on a Firebase object), is there a recommended way to determine if the user is online or offline?

For example:

[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.

}];

We need this in our iOS app because the user could lose connectivity if they went into a tunnel for instance. If Firebase doesn’t offer a built-in way to do this, we’ll just resort to monitoring iOS's Reachability API.

有帮助吗?

解决方案

They have a section of their docs devoted to this here.

Basically observe the .info/connected ref

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

其他提示

You can do something like this. Setup the observer and post notification on status change. Basically the same as accepted answer but adapted to new version of firebase framework.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    FIRDatabaseReference *ref = [[FIRDatabase database] referenceWithPath:@".info/connected"];
    [ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
            NSString *value = snapshot.value;
            NSLog(@"Firebase connectivity status: %@", value);
            self.firebaseConnected = value.boolValue;

            [[NSNotificationCenter defaultCenter] postNotificationName:@".fireBaseConnectionStatus" object:nil];
    }];
}

Then in any view controller of your app you can do this. Observe notifications and do something based on that (update your ui, etc).

- (void) fireBaseConnectionStatus:(NSNotification *)note
{
        AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [self updateButtons:app.firebaseConnected];
}

- (void)viewDidLoad
{
        [super viewDidLoad];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fireBaseConnectionStatus:) name:@".fireBaseConnectionStatus" object:nil];
}

Hope this will help.

PS. Perhaps you will find it interesting idea to also monitor basic reachability with well known reachability.[mh] framework. Then you also could decide how do you act in case that firebase is connected on wifi or 3g.

Swift 3


let connectedRef = FIRDatabase.database().reference(withPath: ".info/connected")
connectedRef.observe(.value, with: { snapshot in
    if let connected = snapshot.value as? Bool, connected {
        print("Connected")
    } else {
        print("Not connected")
    }
})

More info - https://firebase.google.com/docs/database/ios/offline-capabilities

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top