Erik Sapir
Erik Sapir

Reputation: 24717

How can i set connection timeout to NSUrlConnection

I know there is a setTimeoutInterval method to NSMutableURLRequest, but can i set a specific timeout to the time it takes to reach and connect to the server?

Upvotes: 0

Views: 1889

Answers (2)

CouchDeveloper
CouchDeveloper

Reputation: 19106

A NSURLConnection will abort the connection with a timeout error if the connection is "idle" for longer than the specified duration set via setTimeoutInterval.

That means, if you start a request and the client did not receive anything from the server so far, you should get a timeout error in connection:didFailWithError: after that duration.

That also means, if you are in the middle of a connection sending/receiving data, and the server later hangs and the connection becomes "idle" for longer than the specified timeout, it will also abort the connection.

Whenever the connection has some progress, the timer will be reset.

You can tweak that behavior in so far that you start your own timer which sends cancel to the connection after a specific duration. Possibly you may monitor the progress and estimate how long the request will take to finish and then possibly invoke the cancel if that will take to long.

Upvotes: 1

Tala
Tala

Reputation: 8928

No, you can't. Timeout is the time by which we expect the reply from server. We have no idea what time had gone to connect to server and what time for the server to reply.

Upvotes: 2

Related Questions