Search code examples
iphoneobjective-ciosuitableviewmbprogresshud

MBProgress HUD crashing application on reloadData on tableView


I am using this piece of code to display an MBProgressHUD over the top of one of my views while I download data from a web-service, the only problem is that occasionally this code will cause the app to hang, doing nothing while the HUD displays "Downloading" and the screen is locked. Also, if I have something like a keyboard being displayed to the user when I press the refresh button (refresh button performs the download) then the application crashes on the line:

 [self.tableView reloadData];

My code:

//Checks for network connection then displays HUD while executing pullAndDisplayData method
- (IBAction) update {
    UIAlertView *errorView;

    if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
        errorView = [[UIAlertView alloc] 
                     initWithTitle: @"Network Error" 
                     message: @"No Network connection availible!" 
                     delegate: self 
                     cancelButtonTitle: @"OK" otherButtonTitles: nil]; 
        [errorView show];
    }
    else
    {
        HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
        [self.navigationController.view addSubview:HUD];

        HUD.delegate = self;
        HUD.labelText = @"Downloading";
        HUD.minSize = CGSizeMake(135.f, 135.f);

        [HUD showWhileExecuting:@selector(pullAndDisplayData) onTarget:self withObject:nil animated:YES];
    }
}

//Downloads this users data from the web-service
- (void) pullAndDisplayData{
    // Indeterminate mode
    ExpensesDataDownloader *downloader = [[ExpensesDataDownloader alloc] init];
    [downloader pullAndDisplayData];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([[defaults objectForKey:@"canExportCSVServer"] isEqualToString:@"1"])
    {

    }

    [self.tableView reloadData];

    // Switch to determinate mode
    HUD.mode = MBProgressHUDModeDeterminate;
    HUD.labelText = @"Updating";
    float progress = 0.0f;
    while (progress < 1.0f)
    {
        progress += 0.01f;
        HUD.progress = progress;
        usleep(15000);
    }
    // The sample image is based on the work by www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
    // Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = @"Completed";
    sleep(2);
}

Any help would be much appreciated.

Jack


Solution

  • pullAndDisplayData method is running on a separate thread. This is so that MBProgressHUD can use UI thread to show itself. You should always update your UI from main (UI) thread. Use performSelectorOnMainThread: method to call [self.tableView reloadData]; and other UI stuff. I am assuming that [downloader pullAndDisplayData]; is synchronous call.