开发者

conceptual question : what do performSelectorOnMainThread do?

开发者 https://www.devze.com 2022-12-27 13:18 出处:网络
I just come across this strange situation . I was using the technique of lazy image loading from apple examples .

I just come across this strange situation .

I was using the technique of lazy image loading from apple examples .

when I was used the class in my application it gave me topic to learn but don't what was actually happening there .

So here goes the scenario :

I think everybody has seen the apple lazytableimagesloading .

I was reloading my table on finishing the parsing of data :

 - (void)didFinishParsing:(NSMutableArray *)appLis开发者_JAVA技巧t
    {


     self.upcomingArray = [NSMutableArray arrayWithArray:loadedApps];

   // we are finished with the queue and our ParseOperation
 [self.upcomingTableView reloadData];


        self.queue = nil;   // we are finished with the queue and our ParseOperation

    }

but as a result the the connection was not starting and images were not loading . when I completely copy the lazyimageloading and I replaced the above code with the following code it works fine

- (void)didFinishParsing:(NSMutableArray *)appList
{


 [self performSelectorOnMainThread:@selector(handleLoadedApps:) withObject:appList waitUntilDone:NO];


    self.queue = nil;   // we are finished with the queue and our ParseOperation

}

So I want to know what is the concept behind this .

Please let me know if you can not understand the question or details are not enough because I desperately want to know why it is like this ?

LazyTableImages


This method simply invokes a method of your choice but so that it is executed on the main thread. The main thread is, among other things responsible for updating the UI and handling the application main run loop. Even the NSApplication object handle events on the main thread.

Therefore, you use this method by passing as argument the method you want to execute using an Objective-C selector, an object which is meant to represent the input of you method or nil if your method does not require an argument, and finally a boolean value indicating if you want to block until your method is executed or if you instead want to return immediately without waiting for your method to be executed.

If your method needs more than one argument, you wrap all of the required arguments into for instance an NSDictionary and pass the dictionary object as the second argument.

In your case, in order to reload the table, you need to update the UI in the main thread.

0

精彩评论

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