Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MBProgressHUD not disappearing after calling hide

I am sure this is an issue with my iOS/ObjC noob-ness...

I have a UITableView with entries in, when the user selects a row,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
   [self.twitter sendUpdate:[self getTweet]];

and

- (NSString *)sendUpdate:(NSString *)text;
{
    NSLog(@"showing HUD");
    self.progressSheet = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] keyWindow] animated:YES];
    self.progressSheet.labelText = @"Working:";
    self.progressSheet.detailsLabelText = text;

    // Build a twitter request
    TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                              [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] 
                                                 parameters:[NSDictionary dictionaryWithObject:text 
                                                                                        forKey:@"status"] requestMethod:TWRequestMethodPOST];

    // Post the request
    [postRequest setAccount:self.twitterAccount];

    // Block handler to manage the response
    [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
     {
         NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);

           NSLog(@"hiding HUD");
           [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
           self.progressSheet = nil;

It calls the builtin Twitter api to send a tweet. I am using MBProgressHUD while the sending is going on. I am getting erratic behaviour with the HUD disappearance, generally it hangs around 10 or so seconds longer than it should. Based on the show/hide logging I am seeing.

I have another, simpler view, which just lists tweets and that uses the HUD with no problem - although its done via the viewWillAppear call.

Perhaps I need to do the showing via another thread?

Thanks in advance for any thoughts ~chris

like image 585
Chris Kimpton Avatar asked Dec 01 '22 06:12

Chris Kimpton


1 Answers

Yes, you are right about ui thread. You can also write this:

dispatch_async(dispatch_get_main_queue(), ^{
  [self.progressSheet hide:YES];
  self.progressSheet = nil;
});
like image 183
Stan Avatar answered Dec 04 '22 07:12

Stan