I’m using MBProgressHUD in our teaching App, which is tab bar navigated.
The user will come from a tableview via Urban Airship’s storefront directly to the UA detail view.
Once buy is clicked I bring on HUD with
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[self.view.window addSubview:HUD];
It is using the showWhileExecuting statement.
It goes through three while statements to change from “Connecting” to “Downloading” to “Unpacking”.
All working perfectly OK.
Here comes the problem…
The second time I do this the label text will not change. It is stuck on “Connecting”.
I can see in the NSLog that it is going through the other loops.
On top of that, if I try to change the Mode, the app crashes.
This only happens the second time, and any subsequent uses. If I kill the App everything works again for the first time.
Looks to me that MBProgressHUD doesn’t get reset when it’s finished.
(ARC is used in the project)
Anyone with a solution?
Thanks
Edit:
- (void)showWithLabelDeterminate
{
HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
HUD.mode = MBProgressHUDModeIndeterminate;
[self.view.window addSubview:HUD];
HUD.delegate = self;
HUD.labelText = NSLocalizedString(@"Connecting","");
HUD.detailsLabelText = @" ";
HUD.minSize = CGSizeMake(145.f, 145.f);
HUD.dimBackground = YES;
[HUD showWhileExecuting:@selector(lessonDownloadProgress) onTarget:self withObject:nil animated:YES];
}
-(void)lessonDownloadProgress
{
DataManager *sharedManager = [DataManager sharedManager];
// HUD.mode = MBProgressHUDModeIndeterminate;
HUD.labelText = nil;
HUD.detailsLabelText = nil;
while ([sharedManager.downHUD floatValue] == 0.0f)
{
[self parentViewController];
NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.labelText = NSLocalizedString(@"Connecting","");
HUD.detailsLabelText = @" ";
NSLog(@"Waiting for download to start");
// Wait for download to start
usleep(80000);
}
// Switch to determinate mode
// HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = NSLocalizedString(@"DownLoading","");
HUD.progress = [sharedManager.downHUD floatValue];
while (HUD.progress < 1.0f && [sharedManager.cleanedUp isEqualToString:@"No"])
{
// [self parentViewController];
HUD.labelText = NSLocalizedString(@"Downloading","");
NSLog(@"HUD lessonDownloadProgress: %f", HUD.progress);
HUD.progress = [sharedManager.downHUD floatValue];
NSString *percent = [NSString stringWithFormat:@"%.0f", HUD.progress/1*100];
HUD.detailsLabelText = [percent stringByAppendingString:@"%"];
usleep(50000);
}
// Switch HUD while cleanUp
HUD.mode = MBProgressHUDModeIndeterminate;
while ([sharedManager.cleanedUp isEqualToString:@"No"])
{
[self parentViewController];
HUD.labelText = NSLocalizedString(@"Unpacking","");
HUD.detailsLabelText = @" ";
// wait for cleanup
NSLog(@"Waiting for clean up");
usleep(50000);
}
NSLog(@"++ Finished loops ++");
NSLog(@"Finished HUD lessonDownloadProgress: %f", HUD.progress);
[MBProgressHUD hideHUDForView:self.view animated:YES];
[HUD removeFromSuperview];
HUD.delegate = nil;
[HUD release];
HUD = nil;
}
I cannot spot the issue in the code you posted; but some refactoring might help.
Rather than polling the
DataManageryou could use KVO to observe properties on theDataManagerand respond to those changes. (“Don’t call us; we’ll call you.) So here’s a suggested approach if you want.Your class interface:
And in your implementation file…
Above I’ve declared your dataManager as a property so that we can observe it.
To start the download process we now have a method
downloadLesson:Now use KVO to update the appearance of the HUD:
Alternatively, you could use notifications to do the updates, wherein the
DataManagerpostsNSNotifications for which your view controller is registered. Or, if you were open to refactoring theDataManageryou could use blocks to do the updates. All of these solutions avoid having to explicitly block your thread to poll theDataManager. Hope this helps.