How to set a timeout with AFNetworking
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In AFNetworking, timeouts are configured through the underlying NSURLSessionConfiguration or through the request serializer on the manager. The right choice depends on whether you want a default timeout for all requests or a one-off timeout for a specific request.
Set a Default Timeout with Session Configuration
If you want all requests created by an AFHTTPSessionManager to share the same timeout policy, configure the session before creating the manager.
These two values are related but different:
- '
timeoutIntervalForRequestis the timeout for an individual request attempt' - '
timeoutIntervalForResourceis the maximum total time allowed for loading the resource'
For most app code, setting both intentionally is clearer than relying on platform defaults.
Set Timeout Through the Request Serializer
AFNetworking also lets you set the request timeout through the request serializer.
This is a common and convenient approach when you already use a shared manager instance and want to change the default request timeout without rebuilding the session configuration manually.
In many ordinary AFNetworking codebases, this is the most direct answer.
Full Request Example
If the server responds before 20 seconds, the request succeeds normally. If not, the failure block receives the timeout error.
Per-Request Timeout
If only one request needs a different timeout, create an NSURLRequest and set its timeout interval explicitly.
That avoids changing the shared timeout policy for unrelated requests.
Timeout Policy Is an Application Decision
Do not set timeouts arbitrarily. A sensible value depends on the endpoint:
- short timeouts for interactive UI actions
- longer timeouts for large uploads or reports
- separate retry policy if retries are allowed
A single global timeout for everything is convenient, but it is not always the right operational design.
Be Careful with Shared Managers
If your app uses one long-lived AFHTTPSessionManager, changing requestSerializer.timeoutInterval affects future requests created through that manager. That can accidentally change behavior in unrelated code paths.
For that reason, teams often use either:
- one manager per API domain with stable defaults
- a shared manager plus explicit per-request overrides for exceptions
That is safer than mutating a global manager ad hoc from many places in the app.
Recognize Timeout Errors
When a request times out, the error is usually surfaced as an NSURLErrorTimedOut case inside the failure callback.
That lets you distinguish timeouts from DNS failures, certificate problems, and ordinary HTTP error responses.
Common Pitfalls
- Setting the timeout on the wrong object and assuming it affects existing requests.
- Using one shared manager and changing its timeout globally for a one-off call.
- Confusing request timeout with total resource timeout.
- Treating every timeout as a reason to retry automatically.
- Debugging server latency without first checking the client timeout configuration.
Summary
- In AFNetworking, timeouts are usually set through
NSURLSessionConfigurationorrequestSerializer.timeoutInterval. - Use session configuration for stable defaults across a manager.
- Use a custom
NSURLRequestwhen one request needs a special timeout. - Choose timeout values based on endpoint behavior, not convenience alone.
- Handle timeout errors explicitly so they are not confused with other network failures.

