TimeoutInterval in Alamofire - ios

If I set an 5 mins timeout interval for Alamofire request like below, it means an individual/overall API sync would take 5 mins?
sessionConfiguration.timeoutIntervalForRequest = 300
self.defaultManager = Alamofire.SessionManager(configuration: sessionConfiguration, serverTrustPolicyManager: policyManager)

No, it means that max time for timeout response is 300 seconds. If API finishes earlier you won't have to wait that much.
Regarding second question - it depends on your backend. For most cases 60 seconds is more then enough, however if you have any call that exceed it then you need to increase it. Note that it doesn't take parsing time into the consideration, just getting response from server. If you have large object that needs x minutes to parse but get it in 50 seconds it will be fine.
The main reason for timeout is because your server might never response and instead of letting your app hang, you can handle timeout error somehow.

Related

Timeout interval not set for POST requests on iOS

I have a timeOutInterval set to 30 seconds on all my requests via this code:
class DefaultAlamofireSession: Alamofire.Session {
static let shared: DefaultAlamofireSession = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30
configuration.timeoutIntervalForResource = 30
return DefaultAlamofireSession(configuration: configuration)
}()
}
While testing, I noticed that only my GET requests get timed out at 30 seconds. My POST requests are still using the default interval which is 60 seconds.
Can anyone explain why and possibly tell me how I can make the POST requests also time out at 60 seconds?
Thanks a lot,
Paprika
URLSessionConfiguration.timeoutIntervalForRequest does not do what you think it does. From Apple's docs:
This property determines the request timeout interval for all tasks within sessions based on this configuration. The request timeout interval controls how long (in seconds) a task should wait for additional data to arrive before giving up. The timer associated with this value is reset whenever new data arrives. When the request timer reaches the specified interval without receiving any new data, it triggers a timeout.
So that property only controls the timeout between chunks of response Data. That's not typically what you want. Instead, you want to set the URLRequest.timeoutInterval directly, which does more of what you want.
If during a connection attempt the request remains idle for longer than the timeout interval, the request is considered to have timed out. The default timeout interval is 60 seconds.
As you can see, this timeout applies to the connection attempt, which is what most people think of as a request's timeout.
You can customize this value in Alamofire by applying a RequestAdapter to your custom Session. Or, you can use the requestModifier closure that available on the various request* methods.

How to reduce TTFB time under rails application

Under development environment, my app sends GET request which is completed for 10 seconds (Completed 200 OK in 10000 ms ...., is written into console output). But TTFB time is about 40 seconds. GET request receives about 3 MB data.
I arranged response, so the processing is not change but response is empty array and nothing is change about times.
So who uses this time from 'controller finished job' to 'TTFB time, browser receives first byte'
Any explanation?

dataTaskWithRequest takes too long for a very specific string

I'm creating an IOS app using swift.
Recently, I've encountered a weird bug.
Im trying to check if a url is valid, therefore, I'm creating a request with the url and check for the response. I do this task with dataTaskWithRequest of NSUrlSession.
The weird bug is that if the URL is alibaba ,the response returns after a long time(more than 20 seconds sometimes).
Why does that happen?
As far as i concerned it happens only with this specific url.
Here is some code although its not necessary .
let request = NSMutableURLRequest(URL: validatedUrl)
request.HTTPMethod = "HEAD"
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request){ data, response, error in
// The response here returns after a very long time
let url = request.URL!.absoluteString
I would appreciate some help guys!
You're retrieving the contents of a URL over the Internet. The speed at which this happens is arbitrary. It depends on the speed of the DNS server that looks up the hostname, the speed of the web server that responds to the request, the speed of the user's Internet connection, and the speed of every network in between.
You can safely assume that it will either succeed or time out within three minutes. Twenty seconds isn't even slightly unusual over a cellular network.
You should probably rethink what you're doing with this URL and why you're doing it, or at least try to figure out a way to avoid keeping the user waiting while you fetch the URL.

Alamofire slowing down on each request

I am using Alamofire to do a basic requests to an API endpoint. I noticed that the more often I do these tests, the longer the Alamofire request seems to take.
I can reproduce this behaviour with the code sample below. This does a bunch of requests and prints the duration of the request to the console. The last request is about .5 seconds slower than the first one. The amount of slow down seems to be related to the amount of JSON the API returns (our API returns much more data and the slow down is much more significant)
Am I hitting some kind of caching mechanism here?
let testURL = "https://httpbin.org/get"
for var i = 0; i < 100; i++ {
let startDate = NSDate()
Alamofire.request(.GET, testURL)
.responseJSON { response in
print("Duration of request: \(NSDate().timeIntervalSinceDate(startDate))")
}
}
The problem here is not Alamofire, but how you are measuring the latency. You are queueing 100 requests, so the time it takes for each is very small. But since they are queued up, when the last request runs, will depend on the majority of previous requests finishing.
You should use the request timeline object to obtain the latency, with
request.timeline.totalDuration, for example.

How does parse response when there is more request then the limit?

I am working in implementing of parse service.
Suppose we have a free account of parse. So it allow us to send 30 Request/Second.
What happen if there is 40 request sent each second.
will parse respond to 10 request after the 30 request completed or will it reject that request?
if it respond to 10 request after the first 30 and same thing happen for continues 2 min then we have 10 * 120 = 1200 request pending. What happen in this scenario ?
If Prase service going to reject due to this reason(like more request then the limit) then how does we get to know this reason. Is there any error code for this rejection?
From this post:
https://parse.com/questions/getting-this-application-has-exceeded-its-burst-limit-code-155-any-idea
seems that any additional call that exceed the burst limit, will return an error like this:
{"code":155,"error":"This application has exceeded its burst limit."}

Resources