在后台线程中显示UIAlertView [英] Show UIAlertView in background thread

查看:126
本文介绍了在后台线程中显示UIAlertView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从后台线程中的Web服务加载数据。当出现任何问题或在主线程中显示警报视图时,在后台线程中显示 UIAlertView 是否安全?

I load data from a webservice in a background thread. Is it safe to show an UIAlertView in the background thread when anything goes wrong or should I show the alert view in the mainthread ?

感谢您的建议

Frank

推荐答案

从不除主线程外,对GUI执行任何操作。它可能会导致您不想处理的非常奇怪的问题或崩溃。通常回溯也是非常无益的,所以默认情况下尽量避免这样的问题。

Never do anything with the GUI except from the main thread. It can cause very weird issues and or crashes you don't want to deal with. Usually the backtraces are also very unhelpful so try to avoid such issues by default.

因此请使用:

[self performSelectorOnMainThread:@selector(showAlert:) withObject:alertString waitUntilDone:NO];

如果您正在使用Grand Central调度,您可以执行以下操作:

If you are using grand Central dispatch you could do something like:

dispatch_async(dispatch_get_main_queue(), ^{ /* show alert view */ });

更新:

Swift(3.0 +):

DispatchQueue.main.async { // code }

使用通知你也收到了,我曾经有过从不同主题解雇的实例。

It is sometimes helpful to do this with Notifications you receive as well, I have had instances where they were fired from a different thread.

更新2:

看起来苹果已经在iOS11 / Xcode9中添加了一些新工具,以帮助调试在错误的线程上调用内容的问题。

It looks like apple has added some new tools coming in iOS11/Xcode9 to help debug issues where stuff is called on the incorrect thread.

这篇关于在后台线程中显示UIAlertView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆