Skip to main content

Posts

Showing posts with the label Asynchronous Request with NSOperationQueue

How to Handle Multiple Asynchronous Response (Wait to complete All)

Background : Suppose you are developing an application and you need to hit multiple Asynchronous request at a time. Now you are waiting for the responses from all the request to be complete. Question is When you received any response in a delegate, callback or Completion block, how to ensure whether all the request is completed or not ? There can be many ways to do this. I am explaining here two ways to handle this: 1) Use a simple Counter (Preferred for same type of request) 2) Use a enum with Bitwise Method Logical Explanation: Use a simple Counter: In this approach we need to use a counter. For every request we will increase the counter by 1 and when we receive the response, decrement it by 1. This approach is good if we have only one type of request. Like image downloading for gallery. In this approach we cannot differentiate that which request is completed or which is not. Use a enum with Bitwise Method In this approach we will use enum to keep t...

Asynchronous Request with NSOperationQueue

Today post is about how to run a asynchronous task in NSOperationQueue.  Generally we do not run a Asynchronous task in NSOperationQueue. It is also not recommended for any programmer to do that. This post is only for learning purpose what will happen if we schedule a asynchronous task in a queue and how can we complete that task:). So let us move to the learning: NSOperationQueue: In iOS NSOperationQueue is a class, which provide a way to perform operation concurrently. We also have others way to perform concurrent operation: 1) GCD 2) NSThread 3) pThread NSOperationQueue is a wrapper on GCD, which provides a very convenient way to execute operation concurrently. To create a Queue for Operation you have to simply allocate a object of the class: NSOperationQueue * opertionQueue = [[ NSOperationQueue alloc ] init ]; For this post let suppose you are making a queue to handle all Http request in your application. So i want to create a queue in Handl...