Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to perform task in background thread using GCD

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 7.17k
    Comment on it

    Hi Readers,

    GCD stands for Grand Central Dispatch. It can be used to handle background task. In app requirement many time we need to perform some tasks like calculation etc without any effect on main thread. So all we need is to perform that perticular task into background and come again to main thread if needed. GCD can handle background task with dispatch_async as follows:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
            // perform background task like loading data from server like image data
            dispatch_async(dispatch_get_main_queue(), ^{ 
              // perform operation into main thread change user interface like setting downloaded image into imageview  
    
            });
        });

    As you can see with dispatch_async we get a global queue and set its priority accordingly. When we are done with the background task like downloading of an image is completed, we need to set that image into our user interface. That could be done only into main thread so we again switch into the main queue and update user interface.

    Hope it will help.

    Thanks for reading. :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: