Reputation: 21091
I want to make my app more responsive when there are problems with getting data from server.
Right now I am using Reachability library to check access to Internet but i think that it is not enough. Sometimes server can handle request for long time or even be down and not responding.
In cases when network requests take too much time I want to notify user that something went wrong, not showing endless activity indicators.
Where to start from? What are the best practices?
Upvotes: 0
Views: 731
Reputation: 2059
Set a custom timeout interval for the URLRequest. It should be ideally depending on the amount of data you expect the server to send and the internal queries the server does to fetch the data and finally dump the same to the API you are using.
Try using postman to get an idea of server response time and the data you received. Based on that estimate a justified timeout interval for your request.
let request = NSMutableURLRequest()
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.timeoutInterval = 20 // this is what you need to set.
You can then provide message or response to the user through some message. As far as connectivity is concerned, you could look at the YouTube app, try putting device on flight mode and you'll see the strip which shows connectivity status without intruding the user experience.
Upvotes: 2
Reputation: 476
First i would suggest add a loading before checking internet, and dismiss the loading when you received anythings from the URLRequest.
Second, you can set your custom timeout interval for URLSessionConfiguration, so that it wont hang in there
Upvotes: 1