Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to deal with a change in internet connection with HTTP networking

Normally, in my iOS Applications that use quite a lot of HTTP requests to communicate with the server, I add an NSBlockOperation to the app's global NSOperationQueue, and then suspend and enable the queue when the application detects a change in internet connection status thus saving any requests currently in the queue until the queue is unsuspended. However, I am not completely sure that in this way, any operations that are currently running will be stopped, and then re-added to the top of the queue.

My question is: are there any better ways to deal with a change in the network status when you are working with HTTP Requests to a remote server i.e. in pausing, resuming, cancelling requests etc...

like image 611
max_ Avatar asked Feb 13 '13 20:02

max_


People also ask

What are the 4 methods of connecting to the internet?

Internet Connection Types: WiFi, Broadband, DSL, Cable.


1 Answers

You can use the Reachability class to detect when network status changes to avoid sending requests. This is based on, and updated from, some sample code provided by Apple. This will let you know when the internet connection changes without polling.

As far as I know this is the best way to detect network availability. You'll get an instant notification when the network becomes available again. I believe it even tells you what kind of connection is available (WiFi or Cellular).

If the connection drops while making the request, you'll have to handle that condition as you currently are.

like image 96
Luke Avatar answered Sep 29 '22 00:09

Luke