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)