How to send batch request by using AFNetworking 3.0 - ios

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

Related

Doesn't alamofire provide synchronous request?

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

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.

AFHTTPSessionManager calling Multiple Web service or batch with AFNetworking

I need to call multiple web services in a loop for uploading and downloading the data for application.(Known as Sync Process)
I am using AFHTTPSessionManager and create a subclass/wrappper class for that.
I need to do something in a que or creating batch request for that.
I am stuck at this from last night.
Thanks in advance.
For this kind of funcionalities probably an AFHTTPRequestOperationManager is a better choice.
SessionManager relies on tasks, tasks don't have the knowledge of dependency between each other, while operations does.
You have different possibilities:
Wrap sessions into a dispatch_group take a look here
Try to embed a session task inside an NSOperation (really hard)
Use AFHTTPOperation and add dependencies between operations
Use a serial queue on GCD
You might find also this answer helpful

Replacing NSURLConnection with NSURLSession

I have been starting a design for NetworkCommunication. I had a design where NSOperation subclasses create and manage their own NSURLConnection. The NSOperation subclass is instantiated by a NetworkManger class which adds it to the NSOperationQueue. Once the request is finished the delegate(ex:ViewController will be called). This is the flow:
The Network Manger instantiates NSOperation subclass (which encapsulates URL, parameters etc) and adds it to a NSOperationQueue it maintains.
NSOperation subclass instantiates NSURLConnection (which performs asynchronous request and retrieves data)
NSURLConnection dumps data to NSOperation-subclass
NSOperation-subclass executes the completion block supplied by the delegate( view controller).
I'm trying to implement the same pattern with NSURLSession now. I want to be able to encapsulate the url and parameters required to make a network request inside a single object.
Is it fine if i use the same pattern if I use NSURLSession. I checked AFNetworking classes. But, they have not subclassed NSOperation for NSURLSession. And, moreover where should the session object go. Will it be a part of the NSOperation class.
Can someone give some expert advice on this. I should be able to cancel requests, do uploading(POST/PUT), downloading of data. The Network Manager class would be the single point of contact for any network request.
Update:
NSURLConnection is deprecated effective Mac OS 10.11 and iOS 9. So, at this point NSURLSession should be used in lieu of NSURLConnection. As the NSURLConnection.h header says:
Deprecated: The NSURLConnection class should no longer be used. NSURLSession is the replacement for NSURLConnection.
My original answer is below.
The answer depends upon whether you need the richness of the various NSURLSession delegate methods or not. If you're ok using the completion block renditions (i.e. no progress callbacks, no streaming, etc.), the conversion from NSURLConnection to NSURLSession is pretty trivial. Just put your NSURLSession instance in your NetworkManager class, and then wrap the delegate based NSURLSessionTask instances in a concurrent NSOperation subclass and you're done. Just adopt the standard asynchronous/concurrent NSOperation subclass pattern.
If you're using the delegate-based renditions of NSURLSession, it's a whole different kettle of fish. The main hassle is that the various NSURLSessionTask delegate methods are called on the session delegate rather than a task delegate object. At first blush, this might sound like it's a trivial issue, but if your operation instances have unique completion/progress blocks, for example, you're stuck with the hassle of how to have the session object map these delegate method callbacks to the individual original request operation instances.
To tackle this, you have to maintain a mapping of task identifiers to your NSOperation subclass objects. You can then implement these NSURLSessionTask (including task, download task, and upload task) delegate methods at the respective NSOperation subclass. The NSURLSession network manager class can then, when it receives a NSURLSessionTask delegate call, use the task identifier to identify the appropriate NSOperation instance, and then call the appropriate delegate method there.
Finally, if you intend to handle background NSURLSession instances, life gets even more difficult (because background tasks will continue even after your app has terminated and all of its objects have been discarded). Background sessions simply don't lend themselves to an NSOperation-based approach.
Bottom line, this is trivial if you only need the completion-block NSURLSession methods, but it's a bit of a hassle if you need the delegate-based rendition.

NSURLSession has NSoperationqueue internally?

A)If NSURLSession runs task in background in iOS7,Has Apple integrated internally Queue in NSURLSession?How it works in secondary thread and also in App suspended Mode?
B)What is the difference between NSURLSession and NSoperationqueue?
C)If NSURLSession is the replacement of NSURLCOnnection, Can we integrate NSURLSession into
NSOPerationqueue?
D)Are both same?
E)Can we do the same thing in NSURLSession as in NSoperationQueue?
f)is there any time limit to do task in background after closing application? because iOS7 does 2 min while ios6 does 10 min?
G)
Tutorial says under section Configuration and Limitation,NSURLSessionDataTasks are not supported in background sessions at all, and you should only use these tasks for short-lived, small requests, not for downloads or uploads.some tutorials are telling there is no time limit, we can download or upload data,the size may be whatever..Please explain more on this?
If NSURLSession is the relplacement of NSUrlconnection ,Which one is the best in all situations?What is the future of NSUrlconnection?
A) Yes, NSURLSession has its operation queue which maintains all the session tasks delegates in queue.
B) No, NSURLSession and NSOperationQueue has nothing for comparison. In fact, NSURLSession itself has a property of NSOperationQueue. NSURLSession is a replacement-API for NSURLConnection not NSOperationQueue.
C) There is no need of explicitly integrating it to NSOperationQueue. Every NSURLSession object has its own NSOperationQueue to perform its tasks concurrently.
D) Refer A,B,C
E) Again, this is a wrong comparison, NSURLSession is not replacement or equivalent for NSOperationQueue, it is replacement for NSURLConnection.
F) NSURLSession enables to use iOS7 background fetch mode with a background session configuration
[NSURLSessionConfiguration backgroundSessionConfiguration:#"identifier"];
If your question is about general background task execution, in iOS7 you can perform a task in background for 180.0 seconds. But for the background fetch request, you can fetch only for 30.0 seconds
G) Yes, Background fetch is basically designed for downloading/uploading stream of data at regular intervals managed by OS. OS provides the timeframe for your app periodically based on the available system resources. If your app cannot complete the download in 30.0 seconds, with NSURLSession the same task can be resumed next time.
NSURLSession is evolving and it has new priority request model also in iOS8, so I would suggest you to go with NSURLSession. It is asynchronous, it supports authentication handling, it supports separate cache storage, it supports periodic downloads and lot more.

Resources