本文介绍了iOS是在主线程上执行的委托和观察者调用的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,我不确定这里是否使用正确的语言,但是当调用方法因为它们是委托方法,或者因为被列为观察者目标而被调用的方法时,它们是否在主要语言上执行线程?

Sorry, I'm not sure of the right language here, but when methods are called because they are either delegate methods, or methods called as a result of being listed as the target of an observer, are they executed on the main thread?

我想知道我是否可以在这些方法中进行UI更改,或者我是否必须将它们包装在其中

I'm wondering if I can just make UI changes in these methods, or do I have to wrap them in

    dispatch_async(dispatch_get_main_queue(), ^{ UI stuff });

TIA:John

推荐答案

对于代表来说,这可能会有所不同。如果文档没有指定,那么通常它们会在主线程上发送。传统上UIKit必须在主线程上使用,所以这些代表几乎总是从主线程中调用。

For delegates this can vary. If the documentation does not specify, then usually they are sent on the main thread. Traditionally UIKit must be used on the main thread so those delegates will almost always be called from the main thread.

对于通知,我认为你想要这个小片段。

For notifications I think you want this little snip.

来自

最后对于KVO,通知可以来自其他线程。以下是Apple工程师对处理它们的看法。

And finally for KVO, the notifications can come in from other threads. Here is what an Apple Engineer had to say about handling them.

这篇关于iOS是在主线程上执行的委托和观察者调用的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 07:10