Search code examples
iosmbprogresshud

MBProgressHUD not resetting


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;

}

Solution

  • I cannot spot the issue in the code you posted; but some refactoring might help.

    Rather than polling the DataManager you could use KVO to observe properties on the DataManager and respond to those changes. ("Don't call us; we'll call you.) So here's a suggested approach if you want.

    Your class interface:

    @interface YourClass : UIViewController    // or whatever your superclass is...
    {
        MBProgressHUD *_hud;
        DataManager *_dataManager;
    
        //  your other ivars
    }
    @end
    

    And in your implementation file...

    @interface YourClass()
    @property (nonatomic, retain) DataManager dataManager;
    @end
    

    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:

    - (void)downloadLesson;
    {
        //  show HUD and retain it (showHUDAddedTo:animated: returns autoreleased object)
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
        //  observe properties on the dataManager
        [self addObserver:self forKeyPath:@"dataManager.progress" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"dataManager.cleanedUp" options:NSKeyValueObservingOptionNew context:nil];
        [self addObserver:self forKeyPath:@"dataManager.downHUD" options:NSKeyValueObservingOptionNew context:nil];
    
        //  begin your download here...
    
        HUD.labelText = NSLocalizedString(@"Connecting", "");
        HUD.detailsLabelText = @" ";
        HUD.progress = self.dataManager.downHUD;
    }
    

    Now use KVO to update the appearance of the HUD:

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
    {
        if( [keyPath isEqualToString:@"dataManager.cleanedUp"] )
        {
            if( [[[self dataManager] cleanedUp] isEqualToString:@"Yes"] )
            {
                [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES];
                [HUD release];  HUD = nil;
                [self removeObserver:self forKeyPath:@"dataManager.progress"];
                [self removeObserver:self forKeyPath:@"dataManager.cleanedUp"];
                [self removeObserver:self forKeyPath:@"dataManager.downHUD"];
            }
        }
        if( [keyPath isEqualToString:@"dataManager.downHUD"] )
        {
            //  if the data manager updates progress, update our HUD
            HUD.progress = self.dataManager.downHUD;
            if( self.dataManager.downHUD == 0.0 )
                //  no progress; we're just connecting
                HUD.labelText = NSLocalizedString(@"Connecting", "");
            else if( self.dataManager.downHUD < 1.0 )
            {
                //  progress >0.0 and < 1.0; we're downloading
                HUD.labelText = NSLocalizedString(@"Downloading", "");
                NSString *percent = [NSString stringWithFormat:@"%.0f%%", HUD.progress/1*100];
                HUD.detailsLabelText = percent;
            }
            else
            {
                //  progress == 1.0, but we haven't cleaned up, so unpacking
                if( [[[self dataManager] cleanedUp] isEqualToString:@"No"] )
                {
                    HUD.labelText = NSLocalizedString(@"Unpacking","");
                    HUD.detailLabelsText = @" ";
                }
            }
        }
    }
    

    Alternatively, you could use notifications to do the updates, wherein the DataManager posts NSNotifications for which your view controller is registered. Or, if you were open to refactoring the DataManager you could use blocks to do the updates. All of these solutions avoid having to explicitly block your thread to poll the DataManager. Hope this helps.