views:

190

answers:

1

Hello all,

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 *)appList
    {


     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

+3  A: 

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.

unforgiven
So the nsurlconnection only execute If I execute the table reload in main thread , right ?
hib
@hib The NSURLConnection has nothing to do with the table reload. If you have a method that is performed on a background thread, but it needs to update the user interface at some point, that user interface update must be performed on the main thread.
Brad Larson