简体   繁体   English

如果在mainQueue / mainThread之外运行,NavigationController.pushViewController崩溃

[英]NavigationController.pushViewController crashes if run outside of mainQueue/mainThread

I have code like this 我有这样的代码

SomeController *controller = [[SomeController alloc] init];
[self.navController setViewControllers:@[controller] animated:YES];

If run this code in non-main thread , then app crashes during resignFirstResponder of viewController which is executing the above code 如果在non-main thread运行此代码,则在执行上述代码的resignFirstResponderviewController期间应用程序崩溃

If run this code in dispatch_async(dispatch_get_main_queue, ^{}) ; 如果在dispatch_async(dispatch_get_main_queue, ^{})运行此代码;

Then it works fine, no crash 然后工作正常, 没有崩溃

Why does it crash in the first case ? 为什么在第一种情况下会崩溃? I thought if run in non-main thread, then UI wont see the changes but I did not expect it to crash? 我以为如果在非主线程中运行,那么UI不会看到更改,但我没想到会崩溃?

Calls to UIKit classes should take place on the main thread. 对UIKit类的调用应在主线程上进行。 Apple's UIKit documentation states: Apple的UIKit文档指出:

NOTE 注意

For the most part, use UIKit classes only from your app's main thread. 在大多数情况下,仅在应用程序的主线程中使用UIKit类。 This is particularly true for classes derived from UIResponder or that involve manipulating your app's user interface in any way. 对于从UIResponder派生的类或涉及以任何方式操纵应用程序的用户界面的类而言,尤其如此。

Because UIKit assumes that it'll be running on the main thread, there are no real guarantees about what will happen if you try and access it on a different thread; 因为UIKit假定它将在主线程上运行,所以如果您尝试在不同的线程上访问它,将无法真正保证会发生什么。 your UI may not update, you may get a crash, etc. Some classes may even through an exception if they discover you're calling them on a non-main thread. 您的UI可能不会更新,可能会导致崩溃等。某些类甚至在发现非主线程调用它们的情况下甚至可能会通过异常。

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

相关问题 NavigationController.PushViewController也会移动导航栏 - NavigationController.PushViewController moves the navigation bar as well navigationController.pushViewController方法返回未定义 - navigationController.pushViewController the method returns undefined 如何在UITableViewSource下访问NavigationController.PushViewController? - How to access NavigationController.PushViewController under UITableViewSource? MonoTouch: UITableViewController,NavigationController 在 NavigationController.PushViewController 之后是 null - MonoTouch: UITableViewController,NavigationController is null after NavigationController.PushViewController navigationController.pushViewController 似乎在 Xcode 11 中不起作用? - navigationController.pushViewController doesn't seem to work in Xcode 11? iOS NavigationController pushViewController不起作用 - iOS navigationController pushViewController not working 没有导航控制器的 pushViewController - pushViewController without navigationcontroller UIPageViewController中的navigationController pushViewController不起作用 - navigationController pushViewController from UIPageViewController not work navigationController..pushViewController无法启动视图 - navigationController?.pushViewController fails to launch the view NavigationController pushViewController不起作用 - NavigationController pushViewController doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM