I would like to know and understand this code snippet
NSOperationQueue *queue = [NSOperationQueue new];
NSInvocationOperation *operation = [[NSInvocationOperation alloc]
initWithTarget:self
s开发者_开发百科elector:@selector(navigatePage)
object:nil];
[queue addOperation:operation];
[operation release];
[queue release];
-(void)navigatePage
//==================
{
[self performSelectorOnMainThread:@selector(loadPageDetails) withObject:nil waitUntilDone:NO];
[myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
Thanks for your time.
In a nutshell, the code looks to be designed to do some processing in a background thread -- it is probably fetching some data over the network (loadPageDetails
), and then it is updating the UI with the results (reloadData
). However, loadPageDetails
is being called on the main thread, which I don't understand -- surely that should be done a background thread, if it is time consuming?
Can you give a fuller context for your code? I don't really see the point of using NSInvocationOperation
in the above example, because all the operation does is shove more bits of work back on the main thread.
The usual reason for using background processing would be to not block the main thread when doing something that takes time to complete -- I assume the bit of code that sets up the operation queue is called on the main thread?
精彩评论