When observing the replication I always get "offline" as the first notification

When I start observing the couchbase notification for the replicator and start the replicator, the first notification I get is == .offline

I’m using this code to get notified about every change:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeListener:", name: kCBLReplicationChangeNotification, object: pull)

I just wanted to know what can I do to avoid that first notification that seems to be weird. Or what this notification means?

1 Like

It means that the replicator hasn’t yet determined that an Internet connection is available.

If you don’t want it, just ignore it.

Thanks for your quick answer.

How can I differentiate between " the replication hasn’t determined that there is no internet connection" and “the replicator knows there is no internet connection”?

If the Offline state persists for more than a fraction of a second.
What is it you’re trying to do, exactly?

This is what I am doing:

  • Subscribing to the replication notification
  • Start the replicator
  • In the method that handles the replication, I’m just showing/hiding a loader based on the status.
    So, it hides and shows due to that temporary offline status info that comes saying the replicator’s status is == offline

Checking the documentation this is what offline stands for:

    /**< The remote host is currently unreachable. */
    case offline

So, I’m assuming that status is showing up because there is no connection or something similar.
We used your recommendation here https://developer.couchbase.com/documentation/mobile/current/guides/couchbase-lite/native-api/replication/index.html to do that loader process.

So, the main issue is: If we receive “offline” we assume the server is not reachable/noConnectivity and we hide the spinner and display an error message. So, it seems we can’t rely on these statuses to check if the server can be reached or not.

For a future version, it would be nice to have another replicator status named maybe “waitingToLoad” while the replicator is set up. Then if there is an error it could return “offline” or the other statuses if everything is going ok.

1 Like

If we receive “offline” we assume the server is not reachable/noConnectivity and we hide the spinner and display an error message.

If replication is initiated by the user, like by pressing a “Sync” button, that’s OK, but not if it’s just happening normally as part of running the app. CBL is designed to support offline operation, and it would be annoying to have an app pop up an error alert just because you opened it while in an elevator / on the subway / whatever.

What I would do is start a timer when you get the Offline status, and cancel it when you get a different status. When the timer fires, after 5 seconds or so, display the error alert.

Also, note that “offline” means that the device has no Internet connection. Literally it means that no network interface is able to route packets toward the destination IP address. You’ll get this in Airplane Mode, if an iPad has no WiFi connection, or if an iPhone has no WiFi or cellular. It will not happen if the WiFi base station can’t reach the Internet, if your ISP is having problems, or if the server is down. In those cases you’ll get a Stopped status with an error after 30 seconds or so.

Couchbase Lite 2 behaves more like this. There is a new status called “Connecting” that you’ll see while the connection is being established, and you shouldn’t see “Offline” unless there really is no network connectivity.