views:

38

answers:

1

Hi, I am building an iphone app, now this app has a tabviewcontroller and one of the tabs has a navigation view controller in it. Inside the navigation view controller I have 2 UIview controllers and in one I am loading a UITableView. The data for the cells is loaded up from a database and it takes some time, to avoid this I am doing a

[self performSelectorInBackground:@selector(loadTableData) withObject:nil];

Which loads up all the data in a background thread. And inserts the Uitableview as subview. Now the cells in this table fetches images from certain url sources (mentioned in the db), and I am asynchronously fetching these images using a method that I came across online at

http://www.markj.net/iphone-asynchronous-table-image/

By implementing

(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData

And

(void)connectionDidFinishLoading:(NSURLConnection*)theConnection

In a UIView subclass. which downloads the images and puts it in a UIImageView. All this happens inside

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

in After the connectionDidFinishLoading method is fierd this is what supposed to happen

    -(void)connectionDidFinishLoading:(NSURLConnection*)theConnection{
[connection release];
connection=nil;
if ([[self subviews] count]>0) {
    //then this must be another image, the old one is still in subviews
    [[[self subviews] objectAtIndex:0] removeFromSuperview]; //so remove it (releases it also)
}

//make an image view for the image
UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];
//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth || UIViewAutoresizingFlexibleHeight );
for(UIView *view in self.subviews){ //****i do this to remove a UIActivityIndicatorView which is placed until the image is completely fetched 
        [view removeFromSuperview];
}
[self addSubview:imageView];
imageView.frame = self.bounds;
[imageView setNeedsLayout];
[self setNeedsLayout];
[data release]; //don't need this any more, its in the UIImageView now
data=nil;}

this works fine if I don't load the UItableView in the background. i.e when I don't do

[self performSelectorInBackground:@selector(loadTableData) withObject:nil];

But when I do load the table in background the connection:didReceiveData: and connectionDidFinishLoading: methods are never called. But it starts to work properly once I scroll down a few cells.

I hope I have explained things properly here. I am totally confused about this. Sorry about the messy code. I am super noob at this.

A: 

Hey, I don't have an answer to your problem as I am in the very same boat. I asked a similar question yesterday http://stackoverflow.com/questions/3162212/creating-a-single-json-parser-network-object

falconcreek provided me with a few links, not sure if they're of use to you but they may help explain some of your troubles.

I've also started using ASIHTTPRequest ( http://allseeing-i.com/ASIHTTPRequest/ ) for fetching data. More specifically take a look at how the use of queue is demonstrated. Basically create a queue of the requests, once the queue is empty then fire off the next method.

Good luck mate!

cdnicoll