在哪个线程中调用iOS完成处理程序块? [英] In which thread are iOS completion handler blocks called?

查看:114
本文介绍了在哪个线程中调用iOS完成处理程序块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在 GKScore reportScoreWithCompletionHandler 文档),假设你调用

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

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

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

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)?

推荐答案

实际上并不重要。

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

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
    });
}];

这篇关于在哪个线程中调用iOS完成处理程序块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆