33

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tunvir Rahman Tusher
  • 6,421
  • 2
  • 37
  • 32
  • That code looks weird: first `post` is set to `nil`. In the next line a message is sent to `post`. That won't do anything, will it? – Maarten May 16 '13 at 15:52
  • If post == nil then [post dataUsingEncoding:NSUTF8StringEncoding] will return nil as well. – ahwulf May 16 '13 at 17:23

3 Answers3

105

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Marcin Kuptel
  • 2,674
  • 1
  • 17
  • 22
7

If the above code snippets doesn't work then, try this:

Objective-C:

dispatch_async(dispatch_get_main_queue(), ^{

});

UI updates should always be executed from the main queue. The "^" symbol indicates a start of a block.

Swift 3:

DispatchQueue.global(qos: .background).async {
    print("This is run on the background queue")

    DispatchQueue.main.async {
        print("This is run on the main queue, after the previous code in outer block")
    }
}
Md Rais
  • 972
  • 1
  • 12
  • 21
  • What does the ``, ^``do? And for what swift version is this example for? – Sentry.co Dec 23 '16 at 21:57
  • @GitSyncApp, I have updated my answer. Please check it up...:) – Md Rais Jan 23 '17 at 12:25
  • 2
    Just a little clarification: "Sometimes even the background thread wont be able to update the UI and there you have to force the main thread to execute the code into the main queue" this is not right, UI updates MUST be always performed on the main thread, this is not optional at all. – Miguel Rojas Cortés Feb 14 '18 at 18:08
  • 1
    Thats absolutely right @MiguelRojasCortés. I have updated my answer. – Md Rais Feb 15 '18 at 13:02
2

That is a Grand Central Dispatch block.

  1. dispatch_async is a call to run on another queue.
  2. dispatch_get_global_queue is a call to get a specific queue with the desired characteristics. For example, the code could be run at a low priority on the DISPATCH_QUEUE_PRIORITY_BACKGORUND.
  3. Inside the block, the code does nothing. Post is set to nil. Then a message is sent to nil "dataUsingEncoding." Objective C drops all calls to nil. Finally, the parser is sent "nil" postData.
  4. At best, this will do nothing. At worst sending the parser nil data will crash.
rismay
  • 271
  • 2
  • 8