开发者

iPhone - threads

开发者 https://www.devze.com 2023-01-25 23:21 出处:网络
What is the difference be开发者_Python百科tween using this [self performSelectorOnMainThread:@selector(doStuff:)

What is the difference be开发者_Python百科tween using this

[self performSelectorOnMainThread:@selector(doStuff:) 
                       withObject:myObject 
                    waitUntilDone:YES];

instead of simply

[self doStuff:myObject];

in terms of CPU load? Or there are any other advantages?

thanks.


[self performSelectorOnMainThread:@selector(doStuff:) 
                   withObject:myObject 
                waitUntilDone:YES];

This makes sure that the selector is performed on the main thread by adding it to the main run loop. When you set waitUntilDone:YES, you will let your current thread idle as long as the main run loop needs to perform the selector. In a single threaded environment this will let the runloop run right after you called this, in a multithreaded environment, the main runloop will perform the selector once it does the next step.

You have in any of the both cases a small overhead as the selector isn't performed right after you called the function but at a later point while [self doStuff:myObject]; performs the selector immediately


The first one just ensure that your method will be invoked in main thread. If you are not doing multithreading, they should yield same result.


In your example, the second message will be sent straight away, while the first is added to the run loop of the main thread so it won't run immediately.

0

精彩评论

暂无评论...
验证码 换一张
取 消