MBProgressHUD does not disappear after calling hide

I am sure this is a problem with my iOS / ObjC noob-ness ...

I have a UITableView with entries 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; 

He calls the built-in Twitter avi to send a tweet. I am using MBProgressHUD while submitting. I get erratic behavior with the HUD disappearing, it usually hangs for about 10 or more seconds longer than necessary. Based on the show / hide entry, I see.

I have another, simpler view that lists only tweets and it uses the HUD without any problems - although this is done through a viewWillAppear call.

Maybe I need to do a show through another thread?

Thanks in advance for any thoughts ~ chris

+4
source share
3 answers

My problem seems to be that I was trying to close the HUD in a thread other than the main one.

Using the trick in one of the answers to this question, it now works much better.

GCD to complete the task in the main thread

Namely, using the method defined by "runOnMainQueueWithoutDeadlocking"

The closing dialog code now looks like this:

 runOnMainQueueWithoutDeadlocking(^{ NSLog(@"hiding HUD/mainthread"); [self.progressSheet hide:YES]; self.progressSheet = nil; }); 
+5
source

Yes, you're right about the topic. You can also write this:

 dispatch_async(dispatch_get_main_queue(), ^{ [self.progressSheet hide:YES]; self.progressSheet = nil; }); 
+11
source

Try this method to display the HUD:

 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; hud.labelText = @"Working"; 

Instead:

 self.progressSheet = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] keyWindow] animated:YES]; self.progressSheet.labelText = @"Working:"; self.progressSheet.detailsLabelText = text; 

And this is for hiding:

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

Instead:

 [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES]; 
+3
source

Source: https://habr.com/ru/post/1395895/


All Articles