- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
performSelectorOnMainThread:withObject:waitUntilDone:
基於默認模式調用主線程中接收器的方法
Invokes a method of the receiver on the main thread using the default mode.
參數 Parameters
aSelector
一個選擇器, 指定要調用的方法。該方法不應該有明確的返回值並且最多只有一個id類型的參數,或者沒有參數。
A selector that identifies the method to invoke. The method should not have a significant return value and should take a single argument of type id, or no arguments.
arg
需要傳遞給調用方法的參數. 如果沒有需要的參數,傳入nil
The argument to pass to the method when it is invoked. Pass nil if the method does not take an argument.
wait
一個布爾值,指定是否阻塞當前線程直到指定選擇器在主線程中執行完畢。選擇YES會阻塞這個線程;選擇NO,本方法會立刻返回。
A Boolean that specifies whether the current thread blocks until after the specified selector is performed on the receiver on the main thread. Specify YES to block this thread; otherwise, specify NO to have this method return immediately.
如果當前線程也是主線程,選擇YES,消息會立即派發,處理。
If the current thread is also the main thread, and you specify YES for this parameter, the message is delivered and processed immediately.
討論 Discussion
你可以使用本方法向主線程發送消息. 主線程包含應用程序的主循環(這是NSApplication接收事件的地方). 在這種情況下, 消息相當於是你想要在線程上執行的當前對象的方法.
You can use this method to deliver messages to the main thread of your application. The main thread encompasses the application’s main run loop, and is where the NSApplication object receives events. The message in this case is a method of the current object that you want to execute on the thread.
本方法使用Common模式將消息加入到主線程運行循環(run loop)隊列, 即 與NSRunLoopCommonModes常數相關的模式。作為其正常運行循環處理的一部分,主線程從隊列中取出消息(假設它是在Common模式中運行)並調用所需的方法。在同一個線程多次調用此方法會導致相應的選擇進行排隊,再按相同的順序取出執行。
This method queues the message on the run loop of the main thread using the common run loop modes―that is, the modes associated with the NSRunLoopCommonModes constant. As part of its normal run loop processing, the main thread dequeues the message (assuming it is running in one of the common run loop modes) and invokes the desired method. Multiple calls to this method from the same thread cause the corresponding selectors to be queued and performed in the same same order in which the calls were made.
你不能取消由本方法加入隊列的消息. 如果你想取消當前線程的一條消息, 必須使用
performSelector:withObject:afterDelay: 或
performSelector:withObject:afterDelay:inModes: 方法.
You cannot cancel messages queued using this method. If you want the option of canceling a message on the current thread, you must use either the
performSelector:withObject:afterDelay: or
performSelector:withObject:afterDelay:inModes: method.
特別注意事項
Special Considerations
本方法在runloop中注冊當前上下文, 並且依賴於runloop定期運行以保證正確執行. (?????此處翻譯不出, 請大家幫助)。如果你在運行一個調度隊列時需要這種類型的功能, 你應該使用dispatch_after和相關的方法來得到你想要的行為。
This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly. One common context where you might call this method and end up registering with a runloop that is not automatically run on a regular basis is when being invoked by a dispatch queue. If you need this type of functionality when running on a dispatch queue, you should use dispatch_after and related methods to get the behavior you want.