Doesn't alamofire provide synchronous request? - afnetworking

Many times, it's easier to use synchronous code.
Wonder if alamofire supports synchronous request?

Related

How to send batch request by using AFNetworking 3.0

I use AFNetworking 3.0 and I want to send batch request. AFNetworking 2.0 is support enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock not AFNetworking 3.0.
How can I send a batch of requests using AFHTTPSessionManager?
In AFNetworking 3.0 there is not an API to do that. Basically this is because it uses NSURLSession instead of wrapping NSURLConnection into NSOperation.
You have some options:
Use GCD dispatch groups
Make your own implementation for wrapping NSURLSession into NSOperation, just need to make the NSOperation observe the state NSURLSession property and add dependency between operations before adding to the NSOperationQueue
Use promises, you can find a lot of implementations on GITHUB

AFHTTPSessionManager convenience methods (GET, POST, PUT, etc) and background transfers

I've built an app around AFNetworking 2.0's AFHTTPSessionManager and its nice HTTP convenience methods. I now need to ensure that all of this networking functionality can run in the background, and I'm rather concerned.
Reading Apple's documentation, I can see that data tasks are not supported for background sessions. After looking briefly at AFHTTPSessionManager's implementation of GET, POST, PUT et al, it seems to use NSURLSessionDataTask across the board.
Is there something I'm missing, or do I have some redesign and rework to do?
In the event that I'm right (and I suspect I am), and that this codepath won't allow me to background uploads and downloads, is there any reason I couldn't wrap AFURLSessionManager's other methods that use non-data tasks in the same way the current HTTP methods wrap "dataTaskWithRequest:completionHandler"? For example for a POST I could perhaps use "uploadTaskWithRequest:fromData:progress:completionHandler"?
I'm asking, since I'm wondering if this is a viable route, why the AFNetworking devs didn't use it, so that AFHTTPSessionManager's convenience methods would allow for background transfers.
AFNetworking allows you to perform background requests (though be careful to not use any of the task-specific completion blocks and make sure you implement the appropriate app delegate methods; see AFNetworking 2.0 and background transfers). You probably also can use requestWithMethod of AFHTTPRequestSerializer to simplify the process of building the request, too (though, IIRC, you cannot use HTTPBody of the request with background upload requests, so you might have to save the body of the request to a file and then issue your background upload request using that file).
But you're absolutely correct that you cannot use the AFHTTPSessionManager methods to initiate NSURLSessionDataTask tasks when using background session. Regarding why they implemented it that way, that's a question better suited for their issues forum.

Wait for multiple responses then make another call

I have three API calls that I am making with AFNetworking 2.0. The first two are POST requests (/picture) to upload an image and the third request (/message) uses an identification number returned from both of those image POST calls responses. Practically, there is a chance that the user could initiate the third request before I have a response from one of the /image POSTs. I don't want to block the UI. I know AFNetworking is asynchronous and this seems like a synchronous process. Looking for some guidance.
Use AFURLConnectionOperation operation dependencies or batching, or dispatch_group to synchronize behavior once certain conditions are met. Alternatively, dependent calls can be nested into successive completion blocks.

Testing NSURLConnection requests performance with XCTestCase

I'd like to take advantage of the testing classes and methods in Xcode to measure the performance of several asynchronous HTTP services requests my app does. I call services by means of an object that makes use of an NSURLConnection connection and implements the NSURLConnectionDelegate protocol to get the responses. I've been reading the Testing with Xcode document, and a way to test asynchronous operations is described, but it doesn't fit this kind of asynchronous task, since a protocol delegate is involved.
I don't find any example/tutorial of how could I write test methods for these cases, or for cases where asynchronous operations involve protocol methods in general. Any guidance would be appreciated.
I'm using Xcode 6.0.1
Thanks

NSURLConnection synchronous request from a thread vs asynchronous request

What is the differance between adding a operation which make a synchronous NSURLConnection request in NSOperationQueue ( or synchronous request from a thread ( not main thread)) AND making a asynchronous request from the main thread ?
Both will not block main thread so UI will remain responsive but is there any advantage of using one over other? I know in later method i can track request progress etc but assume that progress and other HTTP stuff is not important here.
They are very similar. The biggest problem with synchronous requests is that they can't easily be cancelled. Depending on your application, that could be a problem. Imagine you are downloading a big document and the user moves to another screen so you no longer need that information. In our case, I actually chose doing asynchronous NSURLConnections on a secondary NSThread, which may be overkill for some apps. It is more complicated, but it gives us the ability to both cancel requests and to decode the JSON/XML/image data on secondary threads so they don't impact main thread user interactivity.
Asynchronous requests are scheduled on the run loop and setup as a run loop source, triggering the code automatically only when there is data received from the network (as any socket source).
Synchronous requests running on a NSThread monopolizes a thread to monitor the incoming data, which is in general quite overkill.
You can always cancel an NSURLConnection even if it has been executed asynchronously, using the cancel method.
I bet using the new API that allows to send an asynchronous request on an NSOperationQueue (+sendAsynchronousRequest:queue:completionHandler:) uses GCD under the hood and dispatch_source_create, or something similar, so that it behave the same way as when an NSURLConnection is scheduled on the run loop, avoiding using an additional thread (watch the WWDC'12 videos that explains why threads are evil and their usage should be minimized), the difference only being that allows you to use a block to be informed upon completion instead of using the delegate mechanism.
Some years ago I created a class that embedded NSURLConnection asynchronous calls and delegate management into a nice block API (see OHURLLoader on my github) that makes it easier to use (feel free to take a look). I bet the new API that uses NSOperationQueues uses the same principle, still doing asynchronous requests on the runloop but allowing you to use blocks instead of having to implement a delegate.
The historical position was that there's an advantage in power consumption, and therefore battery life, in asynchronous requests — presumably including both the older delegate approach and the new block-based approach.

Resources