Use AFHTTPRequestOperationManager to know how many network requests in 60 seconds - ios

I have a AFHTTPRequestOperationManager subclass that I use for API calls to an external service. They only allow 30 API calls per 60 seconds, so I need to make sure I keep track of how many I have used.
Is there a way to know each time I have made a request with the operationManager and then keep a timer going so I know how many requests have been made? Then reset the count after 60 seconds? Or should I just save an NSDate object and then check to see if it has been 60 seconds?

Related

Timer management in network requests

I have a question about managing timers in Swift.
Currently I have a ViewController that takes care of calling a rest network service, every 30 seconds, through a Timer that I initialize inside ViewDidLoad with an and with the value repeats: true.
Up to now there are no problems, the functioning seems quite correct to me.
However, I would like to understand a timer feature.
Let's say I decide to decrease the time from 30 seconds to 1 second, in this case, what happens with the network requests? From what I understand the timer, being scheduled every 1 second, this makes 1 request every second, but as we all know, there could be a delay in the response from the backend.
In this case, are subsequent requests queued?
Is there any way to say, wait for the backend response and then execute the second request?

Connect and Complete timeouts for NSMutableURLRequest

We're converting our CURL HTTP Get requests to native IOS code. With CURL we can set two different timeouts - CURLOPT_CONNECTTIMEOUT - how long before a call fails if it cant connect, and CURLOPT_TIMEOUT - how long before a call fails if all the data hasnt been retrieved. If the connect fails we want it to return pretty quickly (10 seconds), but we download large chunks of data possibly on slow connections so we need the completion timeout to be quite large (5 minutes).
How do we set different timeouts using NSMutableURLRequest
Currently we are setting the single timeout like this
[urlRequest setTimeoutInterval:30.0f]
Is there a way to set two separate timeouts, like CURL does ? And which timeout are we currently setting ? The connection timeout or the completion one.
Thanks
shaun
That's a really good question. The documentation on it was unclear to me:
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.
I did find this really helpful post in the Apple Developer Forums, which an Apple employee explains:
The timeoutInterval property is equivalent to
timeoutIntervalForRequest property.
He's referencing the property on NSURLSessionConfiguration, which can be attached to an NSURLSession. If you set the timeoutInterval of an NSURLRequest, it is used as the value for timeoutIntervalForRequest on the configuration. This property's documentation does provide some insight:
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.
The default value is 60.
Based on that, it seems this value is actually neither!

Simultaneous NSDate generation leading to different time intervals

In a server-client game using bluetooth connectivity (GameKit, specifically, for iOS6 compatibility), 1 or more client devices (iPhone/iTouch) send a timestamp to the server device (iPad) when a button on each client is tapped. i.e. each of the clients calls:
[[NSDate date] timeIntervalSince1970]
...and then sends to the server the value as an NSData payload.
When only 1 client is connected, the time being generated and sent to the server is the same as the time of the physical tap. When 2 or more clients are connected, the generated times differ significantly, even if all the clients are tapped at exactly the same time, e.g.
Client 1 and 2 simultaneously tapped:
client 1 timestamp = 1396974546.558433 (time interval since 1970)
client 2 timestamp = 1396974551.274747 (time interval since 1970)
i.e. client 2 is somehow 5 seconds out from client 1. While I'd accept a few milliseconds of difference since truly simultaneous tapping is hard, multiple seconds is unacceptable. Adding additional clients generates a more or less random difference in times, but usually several seconds.
To make things even more mysterious, the timestamps are being generated differently on the client: the timestamps being received at the server are the same as those being generated by the client (i.e. it's not something happening in transit or on receipt).
I'm at a loss for what's causing this. Any ideas?
[[NSDate date] timeIntervalSince1970] is relative to the time that's set on the device, so you can't rely on two devices sending the same time to your server.
I had a similar issue when developing something using Multipeer Connectivity, and ended up using a library called ios-ntp which uses time information from multiple NTP servers along with the latency of the request itself to derive the accurate time. It's worked pretty well for me.
My guess would be the devices are, for whatever reason, off by 5 seconds apart
my reason for saying this is that the
[[NSDate date] timeIntervalSince1970]
usually has sub millisecond accuracy

Fastest way to send several HTTP Post request for iOS?

I need to send around 20 HTTP Post requests in my iOS Application. Right now I am using NSURLConnection and sending the 20 requests one by one, which of course takes a long time. Each connection starts after the previous, taken around 7 seconds to complete all the requests. Is it possible to send these 20 requests simultaneously and receive the JSON result much faster?
You can use NSOperation and NSOperationQueue to prepare all of the requests and push them onto the queue at the same time. Then you can set the concurrent execution limit to determine how many run at the same time. Don't run all 20 at the same time though as you may flood the network and prevent any of the connections from completing properly. Try running 5 concurrently and see how it goes.

withContentsofURL possible to declare a timeout limit?

I have an app with dynamic data and the update method uses arrayWithContentsofURL and dictionaryWithContentsofURL to get the plists from a server in order to update my database.
My problem:
When there is no or not correctly working internet connection on the device this request simply tries to get the data for about a minute before it stops trying and continues execution.
Is there a way to maybe set a timeout for this function?
PS: I know this is probably the worst way to do this and I would be happy if someone could point me in the right direction :) I'm quite new to iOS programming so please be patient.
In my opinion it's best to use an NSMutableURLRequest with it.
Which has a - (void)setTimeoutInterval method. From the documentation:
The timeout interval, in seconds. 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.
Suggest you use an NSURLRequest to send the Request object. Its delegate functions will return you the plist.
You could take this example, about half way on that page it downloads a json object very much the same way as you could fetch a plist.

Resources