What is the significance of WaitUntilDOne in performSelectorOnMainThread?

白昼怎懂夜的黑 提交于 2019-12-03 12:42:42

From Apple docs about waitUntilDone:

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. If the current thread is also the main thread, and you specify YES for this parameter, the message is delivered and processed immediately.

You have to focus on which thread is executing performSelectorOnMainThread. That thread will block and wait until the main thread has completed that selector; say, e.g., that a worker thread needs sending a message to the main thread (e.g., to store some info to some central store which is not thread-safe). The worker might want to block and wait for the operation to complete before issuing another operation of the same kind. So, waitUntilDone come in handy in such case.

Nick Lockwood

If you say NO, performSelectorOnMainThread doesn't block, and will execute the next line of code after the performSelectorOnMainThread immediately without waiting for the main thread to execute your selector.

If you say YES, the thread from which you called performSelectorOnMainThread will block execution at that line until the main thread has executed your selector, then it will continue.

YES means the current thread blocks / waits until the selector is performed, NO to have performSelectorOnMainThread:withObject:waitUntilDone: returns immediately

Generally, you want to avoid blocking the current thread's execution using YES.

If NO, the message will be added to the main thread's run loop's to do list to be performed when the run loop resumes -- that could be soon and practically immediately/concurrently.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!