NSOperationQueue mainQueue vs performSelectorOnMainThread?

前端 未结 1 1284
情深已故
情深已故 2021-02-01 17:15

What\'s the difference between this:

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

and this:

相关标签:
1条回答
  • 2021-02-01 17:59
    [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.

    0 讨论(0)
提交回复
热议问题