Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:51:02+00:00 2026-05-28T20:51:02+00:00

i have one question related with updating UI on main thread using queues. Well,

  • 0

i have one question related with updating UI on main thread using queues.

Well, suppose we create have a UITableView what shows an UILabel with an UIImageView. The UIImage’s are loaded asynchronously in prepareCellfor.. using:

 dispatch_async(t_queue, ^{
   //load image
   //dispatch_async(dispatch_get_main_queue(),^{
     cell.imageView = image;
   }
 });

But while the block is fetching the image the user presses one cell (or the back button on the navigation view controller) an load de DetailViewController for that cell (or goes back in the app).

My question is: what happens when the block launch the main thread to update the imageView for the cell? it’s trying to update an UIView that is not loaded on window or even it can have been unload…

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:51:03+00:00Added an answer on May 28, 2026 at 8:51 pm

    This is a good question. And the answer when using ARC is essentially that the block itself retains the object, so that it will be around later. This is one of those subtle memory gotchas. If the UITableView from which this cell comes is de-allocated and releases all of its cells, this one will be retained (though off-screen) and will have the assignment of cell.imageView = image; completed, then it will be released.

    I am a big fan of controlled experiments and set out to test this, but a UITableView has many moving parts (no pun intended). So I created a very simple experiment, using a simple NSObject subclass as follows:

    @implementation SayHello
    -(void)sayHello{
        NSLog(@"Hello");
    }
    -(void)dealloc{
        NSLog(@"SayHello dead");
    }
    @end
    

    Obviously this class is intended to give me a function to call in a block (sayHello) and will produce an NSLog when de-allocated.

    I ran my test like this:

    SayHello *hello = [[SayHello alloc] init];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        double delayInSeconds = 30.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [hello sayHello];
        });
    });
    

    The 30 seconds gives even the most lazy runloop time to deallocate the “hello” object (if, in fact, it was not retained). But for those 30 seconds the console is silent. After the 30 seconds have expired I get the “Hello” immediately followed by the “SayHello dead” message.

    So how is this a “gotcha”? Well, obviously if you don’t realize that Blocks/ARC are doing this, it could end up keeping around things you thought should be gone. But also with your example of a UITableViewCell; what if your cell is displayed once and sends a request over the network for an image, but while the block is waiting for the image the cell is reused? Now there is a second block with reference to that cell trying to set its image. Well, now you have a race in which the loser will decide what image is displayed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one more question related to this thread. How to decode some number
I have a question related to this one . I don't want to do
I have a question related to this one : I'm trying to attach an
I have a simple question related to one-line programming. First an example: function test(a)
Another question related to this one . I have a List<SortableObjects> that is the
This question is related to this one . I have a page table with
This is related to my previous question , but a different one. I have
I have a question related to Eclipse PDE development. I'm using target definitions to
I have two questions about creating thread safe types in python, and one related
This question is related to UITableView issue when using separate delegate/dataSource , though I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.