Reducing lag when downloading large amount of data from webpage
- by Mahir
I am getting data via RSS feeds and displaying each article in a table view cell. Each cell has an image view, set to a default image. If the page has an image, the image is to be replaced with the image from the article. As of now, each cell downloads the source code from the web page, causing the app to lag when I push the view controller and when I try scrolling.
Here is what I have in the cellForRowAtIndexPath: method.
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
storyLink = [storyLink stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:&error];
NSString *startPt = @"instant-gallery";
NSString *startPt2 = @"<img src=\"";
if ([sourceCode rangeOfString:startPt].length != 0) { //webpage has images
// find the first "<img src=...>" tag starting from "instant-gallery"
NSString *trimmedSource = [sourceCode substringFromIndex:NSMaxRange([sourceCode rangeOfString:startPt])];
trimmedSource = [trimmedSource substringFromIndex:NSMaxRange([trimmedSource rangeOfString:startPt2])];
trimmedSource = [trimmedSource substringToIndex:[trimmedSource rangeOfString:@"\""].location];
NSURL *url = [NSURL URLWithString:trimmedSource];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
cell.picture.image = image;
Someone suggested using NSOperationQueue. Would this way be a good solution?
EDIT:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"FeedCell";
LMU_LAL_FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LMU_LAL_FeedCell" owner:self options:nil];
cell = (LMU_LAL_FeedCell*) [nib objectAtIndex:0];
}
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *untrimmedTitle = [[stories objectAtIndex: storyIndex] objectForKey: @"title"];
cell.title.text = [untrimmedTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
CGSize maximumLabelSize = CGSizeMake(205,9999);
CGSize expectedLabelSize = [cell.title.text sizeWithFont:cell.title.font constrainedToSize:maximumLabelSize];
//adjust the label to the the new height.
CGRect newFrame = cell.title.frame;
newFrame.size.height = expectedLabelSize.height;
cell.title.frame = newFrame;
//position frame of date label
CGRect dateNewFrame = cell.date.frame;
dateNewFrame.origin.y = cell.title.frame.origin.y + cell.title.frame.size.height + 1;
cell.date.frame = dateNewFrame;
cell.date.text = [self formatDateAtIndex:storyIndex];
dispatch_queue_t someQueue = dispatch_queue_create("cell background queue", NULL);
dispatch_async(someQueue, ^(void){
NSError *error = nil;
NSString * storyLink = [[stories objectAtIndex: storyIndex] objectForKey: @"link"];
storyLink = [storyLink stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *sourceCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:storyLink] encoding:NSUTF8StringEncoding error:&error];
NSString *startPt = @"instant-gallery";
NSString *startPt2 = @"<img src=\"";
if ([sourceCode rangeOfString:startPt].length != 0) { //webpage has images
// find the first "<img src=...>" tag starting from "instant-gallery"
NSString *trimmedSource = [sourceCode substringFromIndex:NSMaxRange([sourceCode rangeOfString:startPt])];
trimmedSource = [trimmedSource substringFromIndex:NSMaxRange([trimmedSource rangeOfString:startPt2])];
trimmedSource = [trimmedSource substringToIndex:[trimmedSource rangeOfString:@"\""].location];
NSURL *url = [NSURL URLWithString:trimmedSource];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
cell.picture.image = image;
});
}) //error: expected expression
}
return cell; //error: expected identifier
} //error extraneous closing brace