What is the significance of WaitUntilDOne in perfo

2019-03-26 13:06发布

What is the significance of WaitUntilDOne in performSelectorOnMainThread?

IN what way the YES or NO set to WaitUntilDone can have on the App?

UPDATE:

My Question should have been: In what scenarios do they make difference?

Sergio's answer was the one I was expecting.

4条回答
来,给爷笑一个
2楼-- · 2019-03-26 13:09

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-26 13:25

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

查看更多
甜甜的少女心
4楼-- · 2019-03-26 13:25

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.

查看更多
够拽才男人
5楼-- · 2019-03-26 13:35

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.

查看更多
登录 后发表回答