1

In several interviews I have been asked about handling of connection, web service calls, server responses and all. Even now I am not clear about many things.Could you please help me to get a better idea about the following scenarios?

  1. What is the advantage of using NSURLSessionDataTask instead of NSURLConnection-I have an idea like data loss will not happen even if the connection breaks for NSURLSessionDataTask but not for the latter.But how it works?

  2. If the connection breaks after sending the request to a server or while connecting to server , How can we handle the code at our end in case of NSURLConnection and NSURLSessionDataTask?-My idea is to use Reachability classes and check when it becomes online.

  3. The data we are sending got updated at the server side. But we don't get the response from server. What can we do at our side to handle this situation?- Incrementing timeOutInterval is the only thing that we can do?

Please help me with these scenarios. Thank you very much in advance!!

shallowThought
  • 16,998
  • 6
  • 55
  • 100
iOSManiac
  • 89
  • 9
  • Similar information, maybe helps: http://stackoverflow.com/questions/28105504/what-is-the-biggest-difference-between-nsurlconnection-and-nsurlsession – shallowThought Jan 09 '17 at 11:44
  • @shallowThought: Thank you for your response.Somewhere I read like , NSURLSession allows us to keep the data/ or protect the app from data loss if net connection breaks.Is it correct? Or What are the things we should consider to handle this situation like network connection breaks. – iOSManiac Jan 20 '17 at 13:23

1 Answers1

2

That's multiple questions, really, but I'll try to answer them all briefly.

  1. Most failure handling is the same between NSURLConnection and NSURLSession. The main advantages of the latter are support for background downloads and cancelling groups of related requests.

    That said, if you're doing a large download that you think might fail, NSURLSession does provide download tasks that let you resume the download if your network connection fails, similar to what NSURLDownload used to do on OS X (never available on iOS). This only helps for downloading large files, though, not for large uploads (which require significant server-side support to resume) or other requests.

  2. Your intuition is correct. When a connection fails, create a reachability object monitoring that particular hostname to see when it would be a good time to try the request again. Then, try the request again.

    You might also display some sort of advisory UI to say that you have no Internet connection. (By advisory, I mean something that the user doesn't have to click on and that does not impact offline use of the app any more than necessary; look at the Facebook app for a great example.)

  3. Provide a unique identifier when you make the request, and store that on the server along with the server's response until the client acknowledges receipt of the response (or purge it anyway after some reasonable number of days). When the upload finishes, the server gives you back its response if it can.

    If something goes wrong, the client asks the server to resend the response associated with that unique identifier. Once your client has the data, it acknowledges receipt and the server deletes the response. If you ask the server for the response and it doesn't have one, then the upload didn't really complete.

    With some additional work, this approach can make it possible to support long-running uploads more reliably. If an upload fails, ask the server how much data it got for that identifier, then tell the server that you're going to upload new data starting at the next byte. On the server side, overwrite the old data starting at that byte (just in case some data was still being written when you asked for the length).

Hope that helps.

dgatwood
  • 9,519
  • 1
  • 24
  • 48
  • :Thank you very much for your valuable response:). Could you please elaborate more on 2nd point. How would we identify whether the request is successful or not?Thank you very much in advance. – iOSManiac Mar 17 '17 at 13:31
  • Success typically means getting back an HTTP status code of 200, and then getting as many body bytes as the header said you should get. However, assuming you're grabbing something like JSON, you can also do it by seeing if the JSON parses correctly. If so, then you got the whole thing. – dgatwood Mar 17 '17 at 16:50