Skip to main content

dispatch_async vs dispatch_sync

GCD:

A library to provide support for concurrent code execution on multicore hardware in iOS. It provides an easier mechanism to access multi-thread environment without taking the overhead of lock and unlock shared data, create a thread and perform a task like that.

If we use  NSThread or pthread in the application then we need to take care of so much point. Like if I need to execute 10 tasks concurrently in my application. How much thread pool I require, how to synchronise two tasks while using threads.

It provides methods to add a task in a queue.

1) Dispatch Async
2) Dispatch Sync 

Dispatch Async:

This method is used to add a block for asynchronous execution and return immediately. If you used this method to add a block then it will return immediately and does not wait for execution of the block.

If you do not want to wait for the execution of the block then you can use this method to add operation in a dispatch queue.

- (void) performMyTask{
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Your block task
        for (NSUInteger i=0; i < 100;i++) {
            NSLog(@"Block %lu", (unsigned long)i);
        }
    });
    //Task 1
    NSLog(@"Task1");
}

What will be the output here?

1) Task 1 print first then all block value 0..99
2) First All block value 0..99 and Then Task 1
3) Cannot Determine the behaviour

The solution is Option 3 you cannot determine the behaviour, in this case, it totally depends on when the global queue picks the task and execute it.

Dispatch Sync:

This method is used to add a block for synchronous execution and wait until execution is completed.
i.e. It blocks the current thread If you used this method then the function will not return until your block is not executed fully. If you want to wait for your block execution you can use this method.

- (void) performMyTask{
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Your block task
        for (NSUInteger i=0; i < 100;i++) {
            NSLog(@"Block %lu", (unsigned long)i);
        }
    });
    //Task 1
    NSLog(@"Task1");
}

What will be the output here?

1) Task 1 print first then all block value 0..99
2) First All block value 0..99 and Then Task 1
3) Cannot Determine the behaviour

The solution is Option 2 the behaviour is well defined in this case method will not return until the block is not executed. So all the block value is executed first then the Task1.

What will happen?

If I use dispacth_sync on the main queue in the view controller life cycle method?

- (void) viewDidLoad{
    dispatch_sync(dispatch_get_main_queue(), ^{
        //Your block task
        for (NSUInteger i=0; i < 100;i++) {
            NSLog(@"Block %lu", (unsigned long)i);
        }
    });
    //Task 1
    NSLog(@"Task1");
}

Your application will crash or become unresponsive because of the deadlock cycle. Dispatch block waits for the main queue to be free and the main queue is blocked to execute the dispatched task.

More Dispatch Sync


Comments

Popular posts from this blog

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 ];...

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...

NSThread with Asynchronous Request

NSThread: It is class used in Cocoa framework to create a thread.You have to specify a target and selector to create a new thread in objective c. So the selector or method is main entry point of your thread.  Detail: What happen once the thread execute all the code in the method (Thread main routine)? Thread is terminated after execute all the code in the entry(its main routine) routine. So how we can run an asynchronous request on NSThread ? What is Async request? It is non blocking request which permit the application to do other task side by side as request is in continue. So it enhance parallelism in the application. Now question is how we can prepare a asynchronous request in iOS: - ( void ) startAsyncRequestWithUrl:( NSString *)urlString{     assert (urlString != nil && urlString. length > 0 );     NSURL * url = [[ NSURL alloc ] initWithString :urlString];     NSURLRequest * urlRequest = [[ NSURLReq...