user2195179
user2195179

Reputation:

UIActivityIndicatorView doesn't render

I have reviewed many posts regarding this issue with no success. I have two table view controllers, a source and a destination, both in a navigation controller. When a user taps a cell in the source table view controller, an object is instantiated which makes a web service call that could take several seconds depending on network speed. Once this web service call is made, the segue is executed and navigation moves from the source to the destination. I want an activity indicator to show when this web service call is made. Nothing I have tried for the past several days has worked, even posts marked as successful on this forum. So, there must be something... Here is my code thus far:

Source table view controller header:

@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;

Source table view controller implementation:

@synthesize activityIndicator;

-(void)loadView {
    [super loadView];
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.view addSubview:self.activityIndicator];
    self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    NSString *stateAbbr;

    if([segue.identifier isEqualToString:@"sgShowStateDetail"]){
        DetailTableViewController *detailVC = [segue destinationViewController];
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:path.section];
        NSString *key = [tempArray objectAtIndex:path.row];
        stateAbbr = [statesDict objectForKey:key];
        [detailVC setStateIdentifier:stateAbbr];

        // begin activity indicator here

        [self.view bringSubviewToFront:activityIndicator];
        [activityIndicator startAnimating];

        gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];

        [activityIndicator stopAnimating];
        [detailVC setStateGauges:gauges];

        // end activity indicator here

    }
}

GaugeList is the object that performs the web service call.

Everything works as expected, except there is never an activity indicator. There are no errors. What am I doing wrong? Thanks!

Upvotes: 0

Views: 673

Answers (2)

Pei
Pei

Reputation: 11643

Here is the code using the method tableView: didSelectRowAtIndexPath: rather than prepareForSegue:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view bringSubviewToFront:activityIndicator];
    [activityIndicator startAnimating];

    dispatch_async(dispatch_queue_create("", nil), ^ {

        NSString *stateAbbr;

        DetailTableViewController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailTableViewController"];
        NSArray *tempArray = (NSArray*)[groupedStates objectAtIndex:indexPath.section];
        NSString *key = [tempArray objectAtIndex:indexPath.row];
        stateAbbr = [statesDict objectForKey:key];
        [detailVC setStateIdentifier:stateAbbr];

        gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
        [detailVC setStateGauges:gauges];

        dispatch_async(dispatch_get_main_queue(), ^{
            [activityIndicator stopAnimating];
            [self.navigationController pushViewController:detailVC animated:YES];
        });
    });
}

Upvotes: 0

Mundi
Mundi

Reputation: 80265

I think you might be using the wrong pattern. The call to the web service has to happen in the background, while the activity indicator has to be shown and hidden on the main thread. Thus

__block NSArray *gauges;
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  gauges = [[GaugeList alloc] initWithStateIdentifier:stateAbbr andType:nil];
  dispatch_async(dispatch_get_main_queue(), ^{ 
       [activityIndicator stopAnimating];
       [detailVC setStateGauges:gauges];
  };
};

Upvotes: 1

Related Questions