在哪个线程中调用iOS完成处理程序块?

Jes*_*der 8 iphone multithreading objective-c objective-c-blocks

例如,在GKScore's reportScoreWithCompletionHandler(文档)中,假设你打电话

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

在哪个线程中将调用完成处理程序:主线程,与被调用的线程相同的线程reportScoreWithCompletionHandler,或者不同的线程(可能是实际得分报告完成的线程)?

换句话说,在完成处理程序中完成的工作是否需要是线程安全的(例如,它在哪个线程中完成并不重要)?

小智 6

实际上并不重要.

如果您需要在主线程中运行完成,只需将其发送到主线程:

[score reportScoreWithCompletionHandler:^(NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // do your stuff here
    });
}];
Run Code Online (Sandbox Code Playgroud)