我有一个 IBAction,它调用一个有时会显示来自异步线程的错误警报的函数。它在模拟器中“有效”,但出现此错误:
CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces.
看到该错误后,我意识到我正在尝试从主线程更新 UI,我需要修复它。
这是我正在调用的异步函数:
let queue = DispatchQueue(label: "com.adrianbindc.myApp.myFunction")
queue.async {
self.createArray()
switch self.arrayIsEmpty() {
case true:
// TODO: update on the main thread
self.displayAlert(alertTitle: "Error Title", alertMessage: "Error Message")
case false:
// Do Stuff Off the Main Queue
DispatchQueue.main.async{
switch someArray.count {
case 0:
// TODO: update on the main thread
self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.")
default:
// Do other stuff
}
}
}
}
/**
Utility method to display a single alert with an OK button.
*/
func displayAlert(alertTitle: String, alertMessage: String) {
let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: false, completion: nil)
}
我尝试过的
我尝试在我的 displayAlert 函数前面添加 @objc 并在异步 block 中像这样调用警报函数:
self.performSelector(onMainThread: #selector(ViewController.displayAlert(alertTitle: "Error Title", alertMessage: "Error Message")), with: self, waitUntilDone: false)
但是我在编译器中遇到了这个错误:
Use of instance member 'displayAlert' on type 'ViewController'; did you mean to use a value of type 'ViewController' instead?
非常感谢关于我的错误所在的任何建议。感谢您的阅读。
最佳答案
我走在正确的道路上,但我使用的语法不正确(通常)。当我调用警报函数时,它会在主线程上执行一些操作,这意味着我需要获取主线程。
下面是我在异步 block 中调用警报的方式:
// ** This gets the main queue **
DispatchQueue.main.async(execute: {
self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.")
}
这是成品的样子:
let queue = DispatchQueue(label: "com.adrianbindc.myApp.myFunction")
queue.async {
self.createArray()
switch self.arrayIsEmpty() {
case true:
// ** This gets the main queue **
DispatchQueue.main.async(execute: {
self.displayAlert(alertTitle: "Error Title", alertMessage: "error message.")
})
case false:
switch resultArray.count {
case 0:
// ** This gets the main queue **
DispatchQueue.main.async(execute: {
self.displayAlert(alertTitle: "Another Error Title", alertMessage: "Another error message.")
})
default:
// Do other stuff
}
}
}
This answer帮助我完成了终点线,并提供了更多可能对 Swift 3.0 有帮助的场景。
关于ios - 在 Swift 3.0 中从异步线程调用 UIAlertController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38298182/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里