简体   繁体   English

当用户退出UITableView并且MBProgressHUD在单独的线程中运行时,iPhone应用程序崩溃

[英]iPhone app crashes when user exits UITableView with MBProgressHUD running in separate thread

In my app, I have a UITableViewController which loads calculated data from my Core Data store. 在我的应用程序中,我有一个UITableViewController,它从我的Core Data商店加载计算数据。 This can take quite some time and hangs the main thread, so to give the user visual feedback, I have installed the MBProgressHUD widget which will show a progress indicator (just the spinning wheel) while the calculation method runs in a separate thread. 这可能需要相当长的时间并且挂起主线程,因此为了给用户提供可视反馈,我已经安装了MBProgressHUD小部件,该小部件将显示进度指示器(仅仅是旋转轮),而计算方法在单独的线程中运行。

This is fine, except that the user can still exit the UITableViewController if they think it's taking too long. 这很好,除了用户仍然可以退出UITableViewController,如果他们认为它花了太长时间。 Of course, this is a good thing, except that when the separate thread concludes its operation, it still tries to call its delegate (the UITableViewController) to hide the MBProgressHUD. 当然,这是一件好事,除了当单独的线程结束其操作时,它仍然试图调用它的委托(UITableViewController)来隐藏MBProgressHUD。 This causes a crash because since the user already exited the UITableViewController, it has dealloc 'd and release 'd itself. 这会导致崩溃,因为由于用户已经退出UITableViewController,因此它具有deallocrelease 'd本身。

MBProgressHUD has the following code to try to stop this: MBProgressHUD具有以下代码来尝试阻止此操作:

if(delegate != nil && [delegate conformsToProtocol:@protocol(MBProgressHUDDelegate)]) {
    if([delegate respondsToSelector:@selector(hudWasHidden)]) {
        [delegate performSelector:@selector(hudWasHidden)];
    }
}

However, my app somehow seems to still be running this inner code ( [delegate performSelector:@selector(hudWasHidden)] ) even though the UITableViewController is totally gone--causing the app to crash. 但是,我的应用似乎仍然在运行这个内部代码( [delegate performSelector:@selector(hudWasHidden)] ),即使UITableViewController完全消失 - 导致应用程序崩溃。

Any suggestions? 有什么建议? I am not running with NSZombiesEnabled . 我没有运行NSZombiesEnabled

从您的UITableViewController viewWillDisappear,viewDidDisappear或dealloc方法,设置MBProgressHUD.delegate = nil;

Once the user has exited the table controller, the delegate property of the hud points to a deallocated object (= a memory zone that could contain anything). 一旦用户退出表控制器,hud的delegate属性指向一个解除分配的对象(=一个可能包含任何内容的内存区域)。 That causes the crash when the computing thread ends and tries to send any message to the delegate. 当计算线程结束并尝试向委托发送任何消息时,这会导致崩溃。

In your table view controller dealloc , you must set the hud delegate to nil . 在表视图controller dealloc ,必须将hud委托设置为nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM