0

I am using [NSOperationQueue mainQueue] and NSInvocationOperation in table view cellforRowAtIndexPath method to load images from web in background.But it is not stopping after download image. Here is my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell=[mytable dequeueReusableCellWithIdentifier:@"cell"];


    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }

    imageLabel=(UIImageView *)[cell viewWithTag:5];
    UIImage *image=[dict objectForKey:[appSmallLogos objectAtIndex:indexPath.row]];
    if (image) {
        imageLabel.image=image;
    } else {   
        NSURL *on=[appSmallLogos objectAtIndex:indexPath.row];
        NSString *on1=[appNames objectAtIndex:indexPath.row];


        NSMutableArray *array=[[NSMutableArray alloc] initWithObjects:on,on1,indexPath, nil];
        operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(didGetAlbums:) object:arr];

        q =[NSOperationQueue mainQueue];
        [q addOperation:operation];

    imageLabel.image=[dict objectForKey:on1];
    }

    return cell;
}

//didgetalbums method

- (void )didGetAlbums: (NSMutableArray* )url
{

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
    dispatch_async(queue, ^{
        NSData *data=[NSData dataWithContentsOfURL:[url objectAtIndex:0]];
        UIImage *image1 = [UIImage imageWithData:data];

        dispatch_sync(dispatch_get_main_queue(), ^{
            [dict setObject:image1 forKey:[url objectAtIndex:1]];

        });
    });

    //reload the requested cell after download image
    [self. mytable beginUpdates];
    [self. mytable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[url objectAtIndex:2], nil] withRowAnimation:UITableViewRowAnimationNone];
    [self. mytable endUpdates];    // imageLabel.image=[dict objectForKey:[appSmallLogos objectAtIndex:indexPath.row]];
}
shivam
  • 1,148
  • 1
  • 12
  • 28
  • Similar issue http://stackoverflow.com/questions/6827203/how-to-remove-cancel-nsinvocationoperation-from-nsoperationqueue – Kamleshwar Oct 11 '12 at 06:39
  • You are right.But It's not working.It is going again and again in didGetAlbums method after download image. – shivam Oct 11 '12 at 06:56
  • Shouldn't the `beginUpdates/reloadRows.../endUpdates` stuff be inside the inner dispatch block, after `[dict setObject:image1 forKey:[url objectAtIndex:1]];` ? – Martin R Oct 11 '12 at 07:06
  • It is working well than before but app crashed after some time. @MartinR – shivam Oct 11 '12 at 07:20
  • [dict setObject:image1 forKey:[url objectAtIndex:1]]; app is crashing due to nil value of key.but i am getting this from web service.How to resolve it. – shivam Oct 11 '12 at 07:35

0 Answers0