jclark754
jclark754

Reputation: 1014

GCD and progressHUDs

Using any form of progressHUD (SV, MB, etc) I run into this problem where the HUD is either hidden right after it is displayed or stays there permanently. In the below example, the HUD stays permanently, regardless of if the twitter feed (or page for other view controllers) has finished loading. How can I make the HUD disappear after the task in the separate thread completes?

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

       //Sets the auth key (user or app) for the RMACC Twitter Feed
       STTwitterAPI *twitter = [STTwitterAPI twitterAPIOSWithFirstAccount];

       [twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {

           [twitter getUserTimelineWithScreenName:@"RMACCNewsNotes" count:10 successBlock:^(NSArray *statuses) {

               dispatch_async(dispatch_get_main_queue(), ^{
                   [MBProgressHUD hideHUDForView:self.view animated:YES];
               });

               self.twitterFeed =[NSMutableArray arrayWithArray:statuses];

               [self.tableView reloadData];

            } errorBlock:^(NSError *error){ }];

        } errorBlock:^(NSError *error) {
            [self twitterselfauthrequest];
        }];

    });        
}

Upvotes: 0

Views: 96

Answers (3)

koen
koen

Reputation: 5741

It should be something like this:

[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

// do all your twitter stuff, including the error checking

      dispatch_async(dispatch_get_main_queue(), ^{
         [self.tableView reloadData];
         [MBProgressHUD hideHUDForView:self.view animated:YES];
      });
});

Upvotes: 1

Matteo Gobbi
Matteo Gobbi

Reputation: 17707

  1. It is not secure capture self in a block: use: __weak typeof (self) weakSelf = self; out the first block.

  2. You are calling [self.tableView reloadData]; not in the main thread;

  3. This request: getUserTimelineWithScreenName could finish in an error: in that case you have not log so be sure that it finish with success.

Upvotes: 1

Artem Stepanenko
Artem Stepanenko

Reputation: 3501

I've observed two things that could be a problem:

  1. Are you sure that this line is launched in main thread?

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

  2. Are you sure that this line is launched at all?

    [MBProgressHUD hideHUDForView:self.view animated:YES];

Upvotes: 0

Related Questions