UIAlertController 因在 backgroundThread 中呈现而崩溃 objective C
UIAlertController crash for being presented in backgroundThread objective C
我在以前的应用程序中一直使用UIAlertController
,但这次我遇到了一个奇怪的错误。这是我用来展示它的代码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ALERT_TITLE_STRING message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:OK_STRING style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
一切都很好,但现在我的应用程序在最后一行崩溃了。这是错误:
This application is modifying the autolayout engine from a background
thread, which can lead to engine corruption and weird crashes. This
will cause an exception in a future release.
我没有在我的应用程序中使用 autolayout
。我不知道为什么会收到此错误。
有什么线索吗?
您不能在主线程以外的任何线程上执行 UI 操作。警报控制器将在内部使用自动布局,这就是您看到错误的原因。
改用dispatch_async
从主线程显示警报。
当您尝试在后台线程中执行 UI 任务时会显示此错误。只需将您的代码更改为:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ALERT_TITLE_STRING message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:OK_STRING style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
});
您无法在主线程以外的任何其他线程中处理 UI。
如果您当前在另一个线程中,您可以强制在主线程上执行一个代码块,方法是将它放在以下块中。
dispatch_async(dispatch_get_main_queue(), ^{
//Display your UIAlertController here
});
我在以前的应用程序中一直使用UIAlertController
,但这次我遇到了一个奇怪的错误。这是我用来展示它的代码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ALERT_TITLE_STRING message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:OK_STRING style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
一切都很好,但现在我的应用程序在最后一行崩溃了。这是错误:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
我没有在我的应用程序中使用 autolayout
。我不知道为什么会收到此错误。
有什么线索吗?
您不能在主线程以外的任何线程上执行 UI 操作。警报控制器将在内部使用自动布局,这就是您看到错误的原因。
改用dispatch_async
从主线程显示警报。
当您尝试在后台线程中执行 UI 任务时会显示此错误。只需将您的代码更改为:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:ALERT_TITLE_STRING message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:OK_STRING style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
});
您无法在主线程以外的任何其他线程中处理 UI。
如果您当前在另一个线程中,您可以强制在主线程上执行一个代码块,方法是将它放在以下块中。
dispatch_async(dispatch_get_main_queue(), ^{
//Display your UIAlertController here
});