1

So I am trying to lazyload a user pictures for a custom UITableView (BubbleTableView)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
                [request setHTTPMethod:@"GET"];

                AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                    NSLog(@"Success");
                    UIBubbleTableViewCell *cell = (UIBubbleTableViewCell *)[self.bubbleTableView cellForRowAtIndexPath:indexPath];
                    NSBubbleData *data = [[NSBubbleData alloc] init];
                    data = [cell data];
                    data.avatar = [UIImage imageNamed:@"send.png"];
                    [self.profileImages setObject:image forKey:[self.commUsers objectAtIndex:x]];
                    //Success
                    [cell setData:data];

                } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                    //Failed
                    NSLog(@"ERROR: %@", response);
                }];

                [operation start];

I am trying to change the Avatar to another image and I am not using the image pulled from the net rather just an image I have stored locally (for purposes of narrowing image update problem).

So if I put the

 UIBubbleTableViewCell *cell = (UIBubbleTableViewCell *)[self.bubbleTableView cellForRowAtIndexPath:indexPath];
                NSBubbleData *data = [[NSBubbleData alloc] init];
                data = [cell data];
                data.avatar = [UIImage imageNamed:@"send.png"];
                [self.profileImages setObject:image forKey:[self.commUsers objectAtIndex:x]];
                //Success
                [cell setData:data];

Inside the AFImageRequestOperation Block, the image doesn't update. However, if I put the exact same code outside the Block, it updates the image. I feel like I am missing something on how Blocks work. How do I fix this?

Thanks!

Alan
  • 9,331
  • 14
  • 52
  • 97

1 Answers1

0

Try to run the UI code in the block on the main thread:

if ([NSThread isMainThread]) {
    // We're currently executing on the main thread.
    // We can execute the block directly.
    createBubbleTableViewCell();
  } else {
   //non-blocking call to main thread
   dispatch_sync(dispatch_get_main_queue(), createBubbleTableViewCell);
}

The checking if you are on the main thread is just for case - preventing deadlock. Also you can use dispatch_async for blocking call.

The UI code should be run always on the main thread.

Ondrej Peterka
  • 3,349
  • 4
  • 35
  • 49