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

  • Home
  • SEARCH
  • 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 7712259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:35:07+00:00 2026-06-01T01:35:07+00:00

I have a UITableView in a ViewController class. The ViewController class uses a custom

  • 0

I have a UITableView in a ViewController class. The ViewController class uses a custom dataController (specified in the AppDelegate). In the dataController class I’m fetching some JSON from the web, parsing it to an NSMutableArray, then using that data to populate the UITableView in the ViewController.

This all works great, except there is a noticeable lag when the app starts up since it takes time to get the JSON and work with it. I’d like to show an empty UITableView with an activity indicator while this data is loading. Unfortunately whenever I put the code in the dataController class into a dispatch queue, the UITableView is never populated with data (the data is loaded according to the log). All I see is a blank table.

I guess my main issue is I don’t know how to set up a queue in the dataController class and then update the UI with the data in that queue but in another class.

Relevant code:

from dataController class:

- (void)initializeDefaultDataList {
    NSMutableArray *dataList = [[NSMutableArray alloc] init];
    self.masterDataList = dataList;
    dispatch_queue_t myQueue = dispatch_queue_create("name.queue.my", NULL);
    dispatch_async(myQueue, ^{
        NSString *jsonString = [JSONHelper JSONpostString:@"http://webservice/getData"];

        NSError *jsonError = nil;
        //convert string to dictionary using NSJSONSerialization
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding] 
                                                                    options: NSJSONReadingMutableContainers 
                                                                      error: &jsonError];
        if (jsonError) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), jsonError.localizedDescription);

        NSArray *dataArray = [jsonResults objectForKey:@"d"];

        for (NSString *dataItem in dataArray) {
            [self addDataWithItem:dataItem];
        }
    });
}

from AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
    MyMasterViewController *firstViewController = (MyMasterViewController *)[[navigationController viewControllers] objectAtIndex:0];
    MyDataController *aDataController = [[MyDataController alloc] init];
    firstViewController.dataController = aDataController;
    return YES;
}

from ViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //would this go here?
    dispatch_async(dispatch_get_main_queue(), ^{
        MyObject *objectAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
        [[cell textLabel] setText:objectAtIndex.name];
    });
    return cell;
}

In case you couldn’t tell I’m really new to iOS and Objective C. Any help or hints you can give would be greatly appreciated. I’m not even sure if I’m expressing my question properly – it just seems that what I want to do shouldn’t be this difficult. Thanks!

EDIT

Ok, so maybe this is a life cycle issue. Just realized that anything I set within the async block is nil outside the block, at least it is until it’s too late to make a difference. That’s why cellForRowAtIndexPath is never called – because the masterDataList being passed to the UITableView is empty. Tested this by initializing

__block NSString *s = [[NSString alloc] init];

outside the block, then setting a value inside the block:

s = @"Testing...";

and finally NSLogging the value of s after the block has supposedly run. But obviously the block hadn’t run yet because s was nil.

  • 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-06-01T01:35:09+00:00Added an answer on June 1, 2026 at 1:35 am

    As I discovered in posts such as this one, data set within the async dispatch cannot be used outside the queue. As I understand it, the whole idea of GCD is that it determines when it’s best to run and dispose of data.

    As a result, I ended up splitting up my code so I was only using the DataController class to, well, control data (I know, revolutionary) and moved all the GCD parts to my ViewController. Amended code:

    DataController class:

    - (void)initializeDefaultDataList {
        NSMutableArray *dataList = [[NSMutableArray alloc] init];
        self.masterDataList = dataList;
    }
    

    ViewController class:

    @interface ObjectMasterViewController () {
        __block NSString *jsonString;
    }
    @end
    

    …

    - (void)getJSONString
    {
        jsonString = [JSONHelper JSONpostString:@"http://webservice/getData"];
    }
    

    …

    - (void)initData {
        NSError *jsonError = nil;
        //convert string to dictionary using NSJSONSerialization
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData: [jsonString dataUsingEncoding:NSUTF8StringEncoding] 
                                                                    options: NSJSONReadingMutableContainers 
                                                                      error: &jsonError];
        if (jsonError) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), jsonError.localizedDescription);
    
        NSArray *dataArray = [jsonResults objectForKey:@"d"];
        //loop through array and add items to list
        for (NSString *dataItem in dataArray) {
            [self addDataWithItem:dataItem];
        }
    }
    

    …

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        dispatch_queue_t myQueue = dispatch_queue_create("name.queue.my", NULL);
        dispatch_async(myQueue, ^{
            //initalize service url string
            [self getJSONString];
            dispatch_async(dispatch_get_main_queue(), ^{
                //retrieve data
                [self initData];
                //reload tableView with new data
                [self.tableView reloadData];
            });
        });
    }
    

    Hope this can help someone who might be in the same boat I was in.

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

Sidebar

Related Questions

I have a custom UITableView class for creating shadow effects on all of my
i have an UITableView in my app and i have to load some images
I have an object data of class EquationData . I also have a custom
I have a UITableView populated with custom UITableViewCells. Within those custom cells, I have
Hello, I have this code in my AppDelegate : [window addSubview:viewController.view]; [window makeKeyAndVisible]; return
After parsing JSON data in a Data class, I set the UIViewController's NSArray *headlines
I have a ViewController that contains a UITableView in which the cells are created
I have a ViewController with a nib which have a UIView and a UITableView
I have a UITableView I'm populating with data from CoreData. I have a data
I have a view controller that lists some data in an UITableView . To

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.