0

I am working on app in which I am consuming a lot of web-services, there is specific task for which I need to execute multiple async request simultaneously and I am bit confused while doing this ask as i want to do this task in optimal way as web service takes time while executing. The task is to execute multiple async request simultaneously and return the response to Main using callback. So I want to know what should I use for such task , like GCD, NSOperation or AFNetworking?

How should i make structure for such task.

Please help me out in this.

2
  • I'd use NSURLSession somewhere in there because it automatically pools connections. Commented Oct 14, 2014 at 18:45
  • @MarcusAdams any example ? also keep in mind i want to execute 3 request simultaneously without waiting and after any of executing request finish i want to return response to Main thread using callback. Commented Oct 14, 2014 at 18:46

2 Answers 2

3

I would personally go about it using NSOperation. It doesn't take much time to implement and if this is really the only networking you will be doing AFNetworking maybe overkill.

That being said, if you are not comfortable with NSOperation or NSURLConnection, then AFNetworking might be the way to go.

In my opinion:

NSOperation = super light weight and performs your task to the "T".

AFNetworking = what you need plus 100 other things you don't need.


Per comment:

An example of executing 3 request without dependency among each other could be done with NSOperation like this (pseudo):

NSOperationQueue *myQueue....create NSOperationQueue

FetchProfilePictureOperation *fetchPictureOperation.....Sublcass of NSOperation
fetchPictureOperation.completionBlock = ^{
    dispatch_async(dispatch_get_main_queue(),^{
        NSLog("fetchPictureOperation Completed");
    });
};

FetchProfileIconOperation *fetchIconOperation....Subclass of NSOperation
fetchIconOperation.completionBlock = ^{
    dispatch_async(dispatch_get_main_queue(),^{
        NSLog("fetchIconOperation Completed");
    });
};


FetchProfileDetailsOperation *fetchDetailsOperation....Subclass of NSOperation
fetchDetailsOperation.completionBlock = ^{
    dispatch_async(dispatch_get_main_queue(),^{
        NSLog("fetchDetailsOperation Completed");
    });
};


[myQueue addOperation: fetchPictureOperation];
[myQueue addOperation: fetchIconOperation];
[myQueue addOperation: fetchDetailsOperation];

Here is a reference to NSOperation from the guy who wrote AFNetworking that is helpful.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks for your quick reply, but can you explain what you meant by only networking , and also can you give me example of executing 3 request simultaneously without waiting for other request ?
I've updated my comment. By only networking I was assuming that you had multiple request that would be made in tandem. Such as when a users profile loads you make multiple request for the profile picture, details, icon, etc. If your making POST, PUT, GET, etc. all over the place then maybe going with AFNetworking is the right choice.
I've updated my answer. Although I'm not at a computer with xcode right now but the completionBlock maybe called on the main thread. You can test it with NSAssert([NSThread mainThread], @"This is the main thread"); inside the completion bloc but not in the GCD block.
I would love to answer that but on an iPad and typing an entire explanation on NSOperation subclass would give me carpal tunnel O.o Here is an example I think of what your looking for: ios-blog.co.uk/tutorials/…
They will occur simultaneously without dependency.
|
0

Take a look at NSURLConnection and NSURLSession.

NSURLConnection has both sync and async methods. If you just need "Let me know when it's done" type async, there is a handy new class method +sendAsynchronousRequest:queue:completionHandler:

You pass it an NSURLRequest, and a block of code to execute once it completes. Very easy to use.

NSURLSession is new in iOS 7, and lets you create groups of actions and manage them, kind of like a download manager. I've read about it, but haven't had occasion to use it yet.

There is also the third party library AFNetworking, which provides easy-to-use-wrappers using the above, plus other services.

In any case you don't need to use GCG or NSOperation. The NSURLConnection and NSURLSession classes manage the async services for you, and are more efficient than doing it yourself.

2 Comments

thanks for your answer, but how i ll use NSURLConnection to execute three 3 request simultaneously and once the request is completed , the response should be returned to Main thread using callback. Can you provide any example for that.
Read the docs on the method I posted, +sendAsynchronousRequest:queue:completionHandler'. It runs asynchronously and executes your completion block on whatever queue you provide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.