objective-c – 是performSelector:onThread:withObject:waitUntilDone:ordered?

我有一个Objective-C类,它会旋转后台线程并在其上运行NSRunLoop.我想从主线程向后台线程传递消息(是的,完全来自主线程).为此,我计划使用内置的performSelector:onThread:withObject:waitUntilDone:.

我想知道是否可以安全地假设我以这种方式发送消息的顺序将是它们在后台线程上接收的顺序.显然,如果多个线程正在调用performSelector:onThread:withObject:waitUntilDone:几乎在同一时间,但由于我只是从主线程调用它,我保证有序排队,这个问题没有实际意义.

最佳答案 根据Apple的文档:

This method queues the message on the run loop of the target thread using the default run loop modes—that is, the modes associated with the NSRunLoopCommonModes constant. As part of its normal run loop processing, the target thread dequeues the message (assuming it is running in one of the default run loop modes) and invokes the desired method.

队列通常是有序数据结构.事实上,它没有声明它可以以任何顺序执行,这意味着它们将按顺序执行.

点赞