Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOperationQueue mainQueue vs performSelectorOnMainThread?

What's the difference between this:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [self doSomthing:object];
}];

and this:

[self performSelectorOnMainThread:@selector(doSomething:) withObject:object waitUntilDone:NO]
like image 408
ma11hew28 Avatar asked May 09 '12 02:05

ma11hew28


1 Answers

[self performSelectorOnMainThread:@selector(doSomething:) 
                       withObject:object 
                    waitUntilDone:NO]

Will perform the selector right when it is called. This is what you have to use if you want to affect the UI from a background thread. If you say YES to waitUntilDone it will block the thread until the method has completed.

mainQueue adds that block to the operation queue of the mainthread but does not guarantee when it will be executed. There could be other items in that queue still waiting to execute.

like image 194
Mike Z Avatar answered Oct 20 '22 22:10

Mike Z