Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatch_async and block in iOS

What this piece of code mean?

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{         TMBaseParser *parser=[[TMBaseParser alloc] init];         parser.delegate=self;         NSString *post =nil;         NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];         [parser parseForServiceType:TMServiceCategories postdata:postData];     }); 

please explain it briefly.

like image 865
Tunvir Rahman Tusher Avatar asked May 16 '13 14:05

Tunvir Rahman Tusher


1 Answers

The piece of code in

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  }); 

is run asynchronously on a background thread. This is done because parsing data may be a time consuming task and it could block the main thread which would stop all animations and the application wouldn't be responsive.

If you want to find out more, read Apple's documentation on Grand Central Dispatch and Dispatch Queue.

like image 167
Marcin Kuptel Avatar answered Oct 05 '22 14:10

Marcin Kuptel