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 7170973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:19:09+00:00 2026-05-28T15:19:09+00:00

I have a UITableViewController that when opened displays a table of the following object:

  • 0

I have a UITableViewController that when opened displays a table of the following object:

class {
  NSString *stringVal;
  int value;
}

However, whenever this controller opens, I want it to download the data from the internet and display "Connecting…" in the status bar and refresh the stringVal and value of all of the objects. I do this by refreshing the array in the UITableViewController. However, to do this the UI hangs sometimes or even displays "blank" table cells until the operation has ended. I’m doing this in an NSOperationQueue to download the data, but I’m wondering if there’s a better way to refresh the data without those weird UI bugs.

EDIT:

the UI no longer displays blank cells. This was because cellForRowAtIndexPath was setting nil values for my cellText. However, it still seems somewhat laggy when tableView.reloadData is called even though I’m using NSOperationQueue.

EDIT2:

Moreover, I have two problems: 1. the scrolling prevents the UI from being updated and 2. when the scrolling does stop and the UI starts to update, it hangs a little bit. A perfect example of what I’m trying to do can be found in the native Mail app when you view a list of folders with their unread count. If you constantly scroll the tableview, the folders unread count will be updated without any hanging at all.

  • 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-28T15:19:10+00:00Added an answer on May 28, 2026 at 3:19 pm

    Based on your response in the question comments, it sounds like you are calling [tableView reloadData] from a background thread.

    Do not do this. UIKit methods, unless otherwise specified, always need to be called from the main thread. Failing to do so can cause no end of problems, and you are probably seeing one of them.

    EDIT: I misread your comment. It sounds like you are not updating the UI from a background thread. But my comments about the architecture (i.e. why are you updating in a background thread AFTER the download has finished?).

    You state that “when the data comes back from the server, I call a background operation…” This sounds backwards. Normally you would have your NSURLConnection (or whatever you are using for the download) run on the background thread so as not to block to UI, then call out to the main thread to update the data model and refresh the UI. Alternatively, use an asynchronous NSURLConnection (which manages its own background thread/queue), e.g.:

    [NSURLConnection sendAsynchronousRequest:(NSURLRequest *)
    requestqueue:(NSOperationQueue *)queue 
    completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler];
    

    And just make sure to use [NSOperationQueue mainQueue] for the queue.

    You can also use GCD, i.e., nested dispatch_async() calls (the outer to a background queue for handling a synchronous connection, the inner on the main queue to handle the connection response).

    Finally, I will note that you in principle can update your data model on the background thread and just refresh the UI from the main thread. But this means that you need to take care to make your model code thread-safe, which you are likely to mess up at least a couple times. Since updating the model is probably not a time consuming step, I would just do it on the main thread too.

    EDIT:

    I am adding an example of how one might use GCD and synchronous requests to accomplish this. Clearly there are many ways to accomplish non-blocking URL requests, and I do not assert that this is the best one. It does, in my opinion, have the virtue of keeping all the code for processing a request in one place, making it easier to read.

    The code has plenty of rough edges. For example, creating a custom dispatch queue is not generally necessary. It blindly assumes UTF-8 encoding of the returned web page. And none of the content (save the HTTP error description) is localized. But it does demonstrate how to run non-blocking requests and detect errors (both at the network and HTTP layers). Hope this is helpful.

    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    dispatch_queue_t netQueue = dispatch_queue_create("com.mycompany.netqueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(netQueue, 
                   ^{
                       // We are on a background thread, so we won't block UI events (or, generally, the main run loop)
                       NSHTTPURLResponse *response;
                       NSError *error;
                       NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                            returningResponse:&response 
                                                                        error:&error];
                       dispatch_async(dispatch_get_main_queue(),
                                      ^{
                                          // We are now back on the main thread
                                          UIAlertView *alertView = [[UIAlertView alloc] init];
                                          [alertView addButtonWithTitle:@"OK"];
                                          if (data) {
                                              if ([response statusCode] == 200) {
                                                  NSMutableString *body = [[NSMutableString alloc] initWithData:data 
                                                                                                       encoding:NSUTF8StringEncoding];
                                                  [alertView setTitle:@"Success"];
                                                  [alertView setMessage:body];
                                              }
                                              else {
                                                  [alertView setTitle:@"HTTP Error"];
                                                  NSString *status = [NSHTTPURLResponse localizedStringForStatusCode:[response statusCode]];
                                                  [alertView setMessage:status];
                                              }
                                          }
                                          else {
                                              [alertView setTitle:@"Error"];
                                              [alertView setMessage:@"Unable to load URL"];
                                          }
                                          [alertView show];
                                          [alertView release];
                                      });
                   });
    dispatch_release(netQueue);
    

    EDIT:

    Oh, one more big rough edge. The above code assumes that any HTTP status code != 200 is an error. This is not necessarily the case, but handling this is beyond the scope of this question.

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

Sidebar

Related Questions

I have a UITableViewController with a table view that works perfectly when in portrait
Right now I have an UITableViewController that displays a set of Artist objects sorted
I have a UITableViewController and I want to extend the tableHeaderView of that table
I have 8 cells that are being built in my UITableViewController. I would like
I have a subclass of UITableViewController. I have code that can add/remove a UISearchBar
I am using a UITableViewController which uploads a table. I have a Nib File
I have a UITableViewController that contains a tableView. I want to set its scrollView
I have a UITableViewController that I have specified as a UISearchBarDelegate. Up until now,
I have a UITableViewController that when loaded gets data from a web-service and stores
I have a UIView that is pushing a UITableViewController that is contained inside of

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.