Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which thread are iOS completion handler blocks called?

For example, in GKScore's reportScoreWithCompletionHandler (documentation), suppose you call

[score reportScoreWithCompletionHandler:^(NSError *error) {
   // do some stuff that may be thread-unsafe
}];

In which thread will the completion handler be called: the main thread, the same thread as reportScoreWithCompletionHandler was called, or a different thread (presumably the thread that the actual score reporting is done)?

In other words, does the work done in the completion handler need to be thread-safe (as in, it doesn't matter what thread it's done in)?

like image 327
Jesse Beder Avatar asked Mar 10 '11 23:03

Jesse Beder


1 Answers

In practical terms it doesn't matter.

If you need your completion to run in the main thread, just dispatch it to the main thread:

[score reportScoreWithCompletionHandler:^(NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // do your stuff here
    });
}];
like image 53
mlaster Avatar answered Oct 19 '22 10:10

mlaster