pepe
pepe

Reputation: 9909

MBProgressHUD not showing when reloading data

I load a JSON when my app starts up.

MBProgressHUD correctly shows a spinner while the data is loading.

I also have a refresh button that triggers a reload of JSON - and I'd like it to show the spinner. Although the data is refreshed the spinner does not show.

Any idea what I'm doing wrong?

Here is the relevant code in my ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self fetchPosts];
}

- (IBAction)refresh:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; // NOT WORKING
    [self refreshPosts];
}

- (void)fetchPosts
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]];

        NSError* error;

        posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    });
}

- (void)refreshPosts
{
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString: @"http://mysite.com/app/"]];

    NSError* error;

    posts = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

    dispatch_async(dispatch_get_main_queue(), ^{
        [self.collectionView reloadData];
    });
}

Upvotes: 0

Views: 663

Answers (1)

Ravi
Ravi

Reputation: 8309

Did you try putting the entire code of refreshPosts (and not just the call to reloadData) inside a dispatch block? I would certainly try to see if it works. I think your data download is probably causing a UI freeze which might screw up the HUD.

Upvotes: 2

Related Questions