0

I'm using the MBProgressHUD library in my app, but there are times that the progress hud doesn't even show when i query extensive amount of data, or show right after the processing of data is finished (by that time i don't need the hud to be displayed anymore).

In another post i found out that sometimes UI run cycles are so busy that they don't get to refresh completely, so i used a solution that partially solved my problem: Now every request rises the HUD but pretty much half the times the app crashes. Why? That's where I need some help.

I have a table view, in the delegate method didSelectRowAtIndexPath i have this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    [NSThread detachNewThreadSelector:@selector(showHUD) toTarget:self withObject:nil];
    ...
}

Then, I have this method:

- (void)showHUD {
    @autoreleasepool {
        [HUD show:YES];
    }
}

At some other point I just call:

[HUD hide:YES];

And well, when it works it works, hud shows, stays and then disappear as expected, and sometimes it just crashes the application. The error: EXC_BAD_ACCESS . Why?

By the way, the HUD object is already allocated in the viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    // Allocating HUD
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];

    HUD.labelText = @"Checking";
    HUD.detailsLabelText = @"Products";
    HUD.dimBackground = YES;
}
4

1 回答 1

0

您需要在另一个线程上执行您的处理,否则处理会阻塞 MBProgressHud 绘图,直到它完成,此时 MBProgressHud 再次被隐藏。

NSThread 对于卸载处理来说有点太低级了。我建议使用 Grand Central Dispatch 或 NSOperationQueue。

http://jeffreysambells.com/2013/03/01/asynchronous-operations-in-ios-with-grand-central-dispatch http://www.raywenderlich.com/19788/how-to-use-nsoperations-and -ns操作队列

/* Prepare the UI before the processing starts (i.e. show MBProgressHud) */

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   /* Processing here */

   dispatch_async(dispatch_get_main_queue(), ^{
      /* Update the UI here (i.e. hide MBProgressHud, etc..) */     
   });
}); 

此代码段将允许您在主线程上执行任何 UI 工作,然后再将处理分派到另一个线程。处理完成后,它会返回主线程,以允许您更新 UI。

于 2013-10-09T13:20:06.550 回答