1

I'm pulling data into a UITableView via an XML feed from a Wordpress site. I wanted to display the table with an image if the post contained one and a default image if it did not. So in my

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

method, I have an if statement that looks like this:

if ([elementname isEqualToString:@"content:encoded"]) {
    NSString *firstImageURL = [self getFirstImageUrl:currentStory.content];
    currentStory.imageURL = firstImageURL;
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString: firstImageURL]]];
    currentStory.image = image;
}

This calls getFirstImageURL, which looks like this:

-(NSString *)getFirstImageUrl: (NSString *) html {
    NSScanner *theScanner;
    NSString *imageURL = nil;

    theScanner = [NSScanner scannerWithString: html];

    // find start of tag
    [theScanner scanUpToString: @"<img" intoString: NULL];
    if ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString: @"src=\"" intoString: NULL];
        NSInteger newLoc2 = [theScanner scanLocation] + 5;
        [theScanner setScanLocation: newLoc2];

        // find end of tag
        [theScanner scanUpToString: @"\"" intoString: &imageURL];
    }

    return imageURL;
}

Everything works as it should, but loading the table takes about 5 to 6 seconds and can sometimes take up to 10 seconds, which is not desirable. I was wondering if there is anything I can do to speed up the process of grabbing the first photo.

UPDATE

So after more investigation, it appears that the bottleneck I'm seeing has nothing to do with me downloading images. In fact the actual downloading of images takes no longer than 2 seconds consistently. It looks like the bottleneck happens when I download the RSS feed:

NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 

This consistently takes the longest.

2012-03-30 14:35:11.506 gbllc[883:3203] inside grabDataForFeed
2012-03-30 14:35:11.510 gbllc[883:3203] reached loadXMLByURL
2012-03-30 14:35:11.512 gbllc[883:3203] after stories alloc

**** 5 seconds ****

2012-03-30 14:35:16.568 gbllc[883:3203] after initWithContentsOfURL
2012-03-30 14:35:16.570 gbllc[883:3203] after initWithData
2012-03-30 14:35:16.573 gbllc[883:3203] about to parse

*** I now parse the XML and download images, takes 2 seconds ***

2012-03-30 14:35:18.066 gbllc[883:3203] Parsed successfully

Right after I alloc my data object, I grab the data for parsing. So I guess my original question is no longer valid and I should probably ask if there's a faster way to grab the initial data for parsing or if I should change my model and try to use json or something?

Sonny Parlin
  • 931
  • 2
  • 13
  • 28

2 Answers2

1

That's because you're downloading the image data itself over the network. You need to offload that and do it asynchronously. Have an NSOperationQueue where you can queue up the image download to happen on a separate thread.

Here's a great example of doing just that: http://davidgolightly.blogspot.com/2009/02/asynchronous-image-caching-with-iphone.html

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • Hmm, that's odd ... without knowing more, I'd be inclined to say that if it takes that long to download the XML feed, then it's a problem with that particular server. In theory, requesting the same URL in a browser would take just as long (or probably slightly faster since the desktop is likely to have a faster connection. That being said, you should never do IO (like downloading images) on the UI thread, so you should do the above :-P – Joel Martinez Mar 30 '12 at 19:47
1

I marked Joel's answer as the best answer because he gave me the idea for async downloading of images, but the solution I ended up using is here:

http://howtomakeiphoneapps.com/how-to-asynchronously-add-web-content-to-uitableview-in-ios/1732/

It's by far the easiest and most elegant I've seen after hours of searching.

Sonny Parlin
  • 931
  • 2
  • 13
  • 28
  • Just curious, did you resolve the download bottleneck, or figure out the true source? or did async downloading end up being the fix? – Joel Martinez Apr 01 '12 at 12:10
  • Async downloading helped the second part (no more two second wait after the initial bottleneck) but there's still a good 4 to 5 second wait when doing the initial download of the RSS feed. Not sure why, I'm still looking at it. – Sonny Parlin Apr 01 '12 at 16:03
  • Did you try requesting that same URL in a browser and seeing if it takes about as long? – Joel Martinez Apr 01 '12 at 21:21
  • Yeah, the rss feed comes right up, I think the delay has something to do with the feed being converted to NSData. At least that's what it seems like. – Sonny Parlin Apr 02 '12 at 04:42
  • Okay I know it's been a while, but I wanted to let everyone know that the issue was with the website. Now that the website has been optimized, the rss feed for the app is much faster. – Sonny Parlin May 30 '12 at 18:46