Pages

Running a task in background with dispatch_async()

        In the first part of this series I have introduced the basics of Grand Central Dispatch API, it's uses and its advantages. In this tutorial I will be demonstrating how to use it in your real app. I will be showing example of two functions dispatch_sync() and dispatch_async(). 
        
1.) dispatch_sync() is used for synchronization purpose in your application. It blocks thread on which you use it until block you submit to this function will successfully complete its execution. This function performs lock on the thread you use it on.

Here are two implementations of dispatch_sync():

dispatch_sync(dispatch_get_main_queue()) {
               //your task to execute
                print("Synchronous task which blocks main thread.")


        Above function is just basic demonstrate how dispatch_sync() works. Here what is happening is, you passed main queue as an argument to dispatch_sync() function which is like you are executing this function on main thread. So this will block your main thread until the task you submitted will complete it's execution. It means this function will not return immediately. Of course there are a lot more ways to use this function, on different threads that is. for example:



dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
               //your task to execute
                print("Synchronous task won't block anything.")


        If you use dispatch_sync() function this way this implementation won't block you main thread but synchronize the task on the global concurrent queue. Means if global queue has five task in queue and dispatch_sync() is any of them, since dispatch_get_global_queue() function returns a global concurrent queue which execute each task concurrently irrespective to other then all five will execute concurrently without block anything.

1.) dispatch_async() is used for performing a task in background means executing a task on a different thread than main thread. It won't blocks any thread but return immediately after starting a task. This function creates a thread for the task you submit to it and schedule it for execution.

2.) Here is implementation of dispatch_async():

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
               //your task to execute
                print("Asynchronous task creates thread for this task to complete.")


        Above implementation of dispatch_async() function will create a thread for your task to execute and then return immediately then you task will start it's execution.
        Now if you want to update your app's User Interface after successful completion of background task then you can do this by applying nested dispatch blocks. Here is a particular example.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
               //your task to execute
                print("Asynchronous task creates thread for this task to complete.")
     
                dispatch_async(dispatch_get_main_queue()) {
                          //your task to execute
                          print("Update User Interface.")
               } 


        The above implementation is of nested dispatch blocks in which after execution of task in background you can update user interface by placing your code in dispatch_async(dispatch_get_main_queue()) block.
        As the same way you can nest dispatch_async() and dispatch_sync() blocks.

That's all about dispatch_async() and dispatch_sync() functions. Try applying some combinations you will definately come up with something new.


No comments:

Post a Comment