Tracking the sync status (iOS)

Hi,

I need to track the possible sync outdated issues, so if the device has not been synced for a while (due to a network issues or CouchDB server unavailability) I need to show a sign on the screen indicating that its local database is outdated. I use the next notification to track the replication status:

NSNotificationCenter.defaultCenter().addObserver(self,
selector: “replicationProgress:”,
name: kCBLReplicationChangeNotification,
object: replication)

so inside of the method:

if pull.status == CBLReplicationStatus.Offline {
// here I write the current timestamp that it is offline and show the sign for the user on the screen
}

and I remove the “out of date” sign once the replication changes back to .Idle like:

if pull.status == CBLReplicationStatus.Idle { // etc…

this works great when I simulate a connection drop via turning off the WiFi on the device, however when I am trying to simulate database server connection lost (not the network) by stoping CouchDB server manually the Couchbase Lite (iOS) returns the next warning:

WARNING: CBLSocketChangeTracker[0x7c877660 pbc_dev]: Longpoll connection closed (by proxy?) after 22.9 sec

and the CBLReplicationStatus is not changing to Offline like when I turn of WiFi with my tests.

How can I catch a “Longpoll connection closed” error? Why CBLReplicationStatus is not changing to .Offline automatically?

The offline state means the device has no network connection that’s capable of reaching the server’s host. It doesn’t imply anything about the server, and it’s definitely not the only state that could prevent sync.

Why isn’t it enough to use the transition to idle to detect when the sync finishes? If the last database change is more than X minutes since the last sync finished, then you can warn the user that the data hasn’t been synced for a while.

So my situation:

  1. The application on the device is running constantly (my current app is the barcode scanner)
  2. The server goes down. (WARNING: CBLSocketChangeTracker[0x7c877660 pbc_dev]: Longpoll connection closed (by proxy?) after) this is where I need to show a sign for the user that sync is not working now…
  3. No activity on the device while the server is down, nobody is scanning (adding a new scan data) (the Replication is Idle as I understand)
  4. Some new data on the server has been added, but the device can’t sync it (no connection to the server and I can’t catch this error CBLSocketChangeTracker and the replication connection status is still Idle…).

btw how can I get the last database change if I don’t have a connection to the CouchDB server?

so if CBLReplicationStatus is not switching Offline when “CBLSocketChangeTracker[0x7c877660 pbc_dev]: Longpoll connection closed (by proxy?)” I need a way to catch this CBLSocketChangeTracker exception, so I can save a timestamp in the app when the server went offline and the app can’t sync the local data…

If you want finer-grained error information, don’t use a continuous replication. A one-shot will fail when it’s unable to connect to the server. You’ll need to add your own logic to schedule replications, though.

ahh interesting, though it would be nice to track the sync availability with the continues replication (similar way like it is now when there is no network connection), but if I’ll stop using it and setup to do a sync let say every min then yeah, I can rely on CBLReplicationStatus…

Thanks!