How to detect network failures, so that failure can be handled properly?

54 views
Skip to first unread message

SeanDev

unread,
Mar 20, 2015, 6:08:24 AM3/20/15
to mobile-c...@googlegroups.com
Hi all,

In mobile apps reachability is always a problem.

I can see there be failure of replication of 401 unauthorized and fail to connect to server and so on.

Is there a class we can use to monitor the failures of the network or the authroization status?

Thanks in advance.

-Sean

James Nocentini

unread,
Mar 20, 2015, 6:34:56 AM3/20/15
to mobile-c...@googlegroups.com
For the unauthorized case (401), you can check the status code.

For reachability, the Replication class has a status property which can be Stopped, Offline, Idle or Active.

You can read more about it here http://developer.couchbase.com/mobile/develop/guides/couchbase-lite/native-api/replication/index.html#observing

- James

SeanDev

unread,
Mar 20, 2015, 8:27:20 AM3/20/15
to mobile-c...@googlegroups.com
Thanks, James. 

The link is really helpful. I believe that is the way to track it.

And for http status code, can we be more specific?
 
For the unauthorized case (401), you can check the status code.

For this one, actually I get this error message, but I don't know how can I get notified before (or just after) the following message happens.

2015-03-20 17:53:21.265 DemoProject[342:66956] WARNING: CBL_Puller[http://127.0.0.1:4984/votings]: Unable to save remote checkpoint: Error Domain=CBLHTTP Code=401 "401 unauthorized" UserInfo=0x170c78d80 {NSURL=http://127.0.0.1:4984/votings/_local/41ac43b2653ee4e94f16092bfeea9b9c486e7c1a, NSLocalizedFailureReason=unauthorized, NSLocalizedDescription=401 unauthorized}



-Sean

James Nocentini

unread,
Mar 20, 2015, 8:53:14 AM3/20/15
to mobile-c...@googlegroups.com
Ok, you could add an observer to the replication change notification on iOS.

And check if the change notification is an error and get the status code from there.

See this example in Todo Lite Android to check if the username/password are valid when logging in with Basic Auth https://github.com/couchbaselabs/ToDoLite-Android/blob/master/ToDoLite/src/main/java/com/couchbase/todolite/Application.java#L291

It should work  the same on iOS.

SeanDev

unread,
Mar 20, 2015, 11:54:40 PM3/20/15
to mobile-c...@googlegroups.com
Yes, thanks James, I think I got that.

For anyone comes into this problem, this is how we, following James's answer, implemented the monitor of http status code in iOS:
  1. add an observer to the replication created, for keyPath @"lastError"
  2. in the observation handling code, check if the error domain of this @"lastError" is @"CBLHTTP"
  3. then check the code property of this @"lastError", possible values are the same as HTTP status code standard. For example, 401 means not authorized. We handle this error to log out current user.
-Sean

James Nocentini

unread,
Mar 21, 2015, 9:31:45 AM3/21/15
to mobile-c...@googlegroups.com
Great, thanks for sharing this.

- James
Reply all
Reply to author
Forward
0 new messages