iOS 上的上下文切换使用“performSelectorOnMainThread”
我在 iOS 上遇到奇怪的线程问题。考虑这样一个简单的方法:
- (BOOL)doSomething
{
[self doA];
[self doB];
}
该方法应该仅在主线程的上下文中运行。我需要从工作线程调用此方法。为此,我使用performSelectorOnMainThread。
如果我按照这里解释的做所有事情。 doA
和 `doB
之间可以发生上下文切换吗?
(我不这么认为,我只是想确保我的理解是正确的)
I have weird threading issues on iOS. Consider a simple method like this:
- (BOOL)doSomething
{
[self doA];
[self doB];
}
This method should only run in context of the main thread. I need to call this method from a worker thread. I use performSelectorOnMainThread
for this purpose.
If I do everything as explained here. Can a context switch happen between doA
and `doB
?
(I don't think so, I just want to make sure that my understanding is right here)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过“上下文切换”,我假设您的意思是主线程切换到其他一些主线程事件(因为您始终可以随时切换到另一个工作线程)。
但是,主线程将在执行其他操作之前完成所有 doSomething 操作。
摘自 performSelectorOnMainThread:
By "context switch" I assume you mean the main thread switching to some other main thread event (as you could always switch to another worker thread at any time).
However, main thread will finish all of doSomething before doing anything else.
Excerpt from performSelectorOnMainThread: