esvendsen
esvendsen

Reputation: 1510

Populate UITableView in ViewController class from separate DataController class that is using grand central dispatch

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.

Upvotes: 0

Views: 848

Answers (2)

esvendsen
esvendsen

Reputation: 1510

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.

Upvotes: 1

rickster
rickster

Reputation: 126177

It looks like you're doing the right thing to get back on the main thread after your work is done, but you haven't told the table view it needs to show the new data. [self.tableView reloadData] ought to help.

Upvotes: 1

Related Questions