Skip to main content

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 track of the request. Enum will have value as particular bit position is set.

Like 
typedef enum{
        request1 1<<0   //First bit can be set using this
        request1 1<<1   //Second bit can be set using this
        request1 1<<2   //Third bit can be set using this
}request;

We will take an integer and use '|' operand to set particular bit, Once we have the response.

Pros:  We can even track whether particular request is completed or not. By using "&" operand with integer to check value is set or not.

Implementation:

Use a simple Counter:

Let suppose you have a url connection custom class. Using that you are hitting a web service asynchrony. You need to take a counter and increment it when you are creating a request.  

You have a controller in which you need to download the image from the url and you have array of image url list. Then your implementation can be following:

ViewController.h

@interface FlickerViewController : UIViewController

@property (nonatomic, strong)NSArray*   imageURLList;

@end

ViewController.m

#import "FlickerViewController.h"

@interface FlickerViewController ()

@property (atomic, assign)NSUInteger requestCount;

@end

@implementation FlickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.requestCount = self.imageURLList.count;
    for (NSString* imageUrl in self.imageURLList) {
        //request for image downloading
    }
}

//Completion block
- (void) downloadManager:(id)manager didCompleteWithResponse:(id)response{
    self.requestCount--;
    if(self.requestCount == 0){
        //Congrats your all request completed
    }

}


Your completion implementation can be different you can use blocks as well but request complete check logic will be same. Yes for thread safety i am using atomic for the property. You can apply other method as well.

Use a enum with Bitwise Method


Let Suppose while loading a view controller you need to fetch user info and user friend list asynchronously. You need to wait until both the request complete. If user info request failed then you need to show error alert. If user friend list fail then no need to show anything just do not display the user friend list.


ViewController.m

#import "FlickerViewController.h"

typedef enum {
    UserDataRequestUserInfoComplete =  1<<0,
    UserDataRequestUserFriendListComplete =  1<<1,
    UserDataRequestUserInfoFailed   =  1<<2,
    UserDataRequestUserFriendListFailed   =  1<<3
}UserDataRequestStatus;

@interface FlickerViewController ()

@property (nonatomic, assign)NSUInteger requestComplete;

- (void) loadUserInfo;
- (void) loadUserFreindList;

@end



@implementation FlickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //send Async request for laoding
    [self loadUserInfo]; //Async Request
    [self loadUserFreindList]; //Async Request
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) loadUserInfo{
    
    __weak __typeof(self) weakSelf = self;
    
    [ServerManager loadUserInfoWithCompletionBlock:^(id response, BOOL isError, NSError* error){
        if(isError ||  error != nil){
            weakSelf.requestComplete |= UserDataRequestUserInfoFailed;
            //Do logic to show alert
            return;
        }
        if([weakSelf isAllRequestSucceed]){
            //Do your logic
        }else if(self.requestComplete & UserDataRequestUserFriendListFailed){
            //reload page with data
        }
    }];
    
}
- (void) loadUserFreindList{
    [ServerManager loadUserFriendListWithCompletionBlock:^(id response, BOOL isError, NSError* error){
        if(isError ||  error != nil){
            weakSelf.requestComplete |= UserDataRequestUserFriendListFailed;
            return;
        }
        if([weakSelf isAllRequestSucceed]){
            //Do your logic after all complete
        }else if(self.requestComplete & UserDataRequestUserInfoFailed){
            //Do not display anything
        }
        
        
    }];
}

- (BOOL) isAllRequestSucceed{
    if((self.requestComplete & UserDataRequestUserInfoComplete) && (self.requestComplete & UserDataRequestUserFriendListComplete)){
        return true;
    }
    return false;
}

@end

In the above case we use completion block and check if all the request is completed then display the view.

There will be more ways to handle the multiple Asynchronous response. If you have better approach then we can discuss it here.

Please provide your comments on this and let me know if any help required.




Comments

Popular posts from this blog

iOS8 UIWebView Remove or Modify Keyboard Toolbar

Remove Toolbar: One of my client requirements is to remove the default toolbar for the keyboard. I found numerous examples for iOS7 to remove toolbar from the keyboard window. But it did not work for iOS8. So i came to following solution after some Research on Google: Step1:   First we need to add observer for keyboard notification: [[ NSNotificationCenter defaultCenter ]  addObserver : self selector : @selector ( removeKeyboardTopBar :) name : UIKeyboardWillShowNotification object : nil ]; Step 2: We need to implement the method for observer: - ( void )removeKeyboardTopBar {     } Step 3: Find the keyboard window from all the application  windows:   - ( void )removeKeyboardTopBar {     UIWindow *keyboardWindow = nil ;     UIView * toolBarContainer = nil ;     NSArray * windows = [[ UIApplication sharedApplication ] windows ];     for ( U...

Implement orientation modes in iPhone Hybrid Applications

Let suppose you are working on a hybrid application which runs only in single (portrait) mode. One day a requirement come that PDF and Doc Viewer (HTML Page) should support both (landscape, portrait) mode. Your Application loads all the HTML content from the local html files and you need to implement the above functionality only for one HTML file. Let break the above task in the modules: Step 1: Application should detect when the PDF and Doc viewer is open in application. I setup location.href tag in html to " docvieweron:// " and " docvieweroff:// " when page is open and closed respectively. In this way I am getting a delegate callback in web view: WebViewDelegate: - ( BOOL ) webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request   navigationType: ( UIWebViewNavigationType )navigationType {          NSString * urlString = [[request URL ] absoluteString ];...

Blocks are Objective-C Objects !!!

Blocks Blocks are executable code just like a function.  It can be written inside a function or we can store the reference in a variable and call it later.  But wait a minute these all functionality we can achieve by function pointer and functions.  Then what is so special about blocks? Let first go through how we can define blocks. int (^sum)( int , int ) = ^( int number1, int number2){ return number1+number2; }; The above block can be used to find the sum of two numbers.  If we recall the function pointer in c then it is very much similar to above: int sumFunction ( int number1, int number2){ return number1+number2; } int (*sum) ( int , int ) = sumFunction; //Function Pointer *sum Then Why we need Blocks if we already have Function pointer?  1) B locks can capture variables from the enclosing scope by simply referring to them within the block. How ? Let see the next Section 2)  Blocks...