Search code examples
objective-cgrand-central-dispatchdelegation

GCD and delegation on which thread?


I am trying to figure out if this code is updating the UI on the main thread. This code is in a class I made specifically for downloading the JSON feed. After getting the data, I parse it to return an array I will use to update the UI, and then let the delegate know the data is ready. My delegate, a view controller, will use this array to update the UI. Do I need to do the -parser:didFinishParsingWithResults: in a dispatch_get_main_queue block? Or is that not necessary since I am updating UI in the view controller class and not this class? Thank you!

__block NSArray *results;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *responseData = [NSData dataWithContentsOfURL:url];
    results = [self parseData:responseData];
    [self.delegate parser:self didFinishParsingWithResults:results];
});

Solution

  • dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    

    That ain't the main thread. That is the default priority global queue which might be any thread.

    If parser:didFinishParsingWithResults: diddles UI elements without dispatching to the main queue / thread, then you have a problem.