我有以下代码片段,其中有时这一行:self.onPostExecute(transItem)
导致应用程序崩溃:
func execute(){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//Your code to execute in background...
println("Your code to execute in background...")
var transItem:WmTransferItem = self.doInBackground()
dispatch_async(dispatch_get_main_queue(), {
println("code to be executed on the main thread when background task is finished")
self.onPostExecute(transItem) // line 639
});
});
}
这是什么意思:带有未损坏的后缀“_promote0”
异常(exception):
Thread : Crashed: com.apple.main-thread
0 MyApplication 0x000e9ed8 MyApplication.WmBuildGroupsTask.onPostExecute (MyApplication.WmBuildGroupsTask)(MyApplication.WmTransferItem) -> () (WmBuildGroupsTask.swift:419)
1 libswiftCore.dylib 0x0045803b swift_reportFatalError + 162
2 MyApplication 0x000f2ddc MyApplication.WmBuildGroupsTask.(execute (MyApplication.WmBuildGroupsTask) -> () -> ()).(closure #1).(closure #1) with unmangled suffix "_promote0" (WmBuildGroupsTask.swift:639)
3 MyApplication 0x000f2e34 reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) (WmBuildGroupsTask.swift)
4 libdispatch.dylib 0x3a133d53 _dispatch_call_block_and_release + 10
5 libdispatch.dylib 0x3a133d3f _dispatch_client_callout + 22
6 libdispatch.dylib 0x3a1366c3 _dispatch_main_queue_callback_4CF + 278
7 CoreFoundation 0x2f47a641 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
8 CoreFoundation 0x2f478f0d __CFRunLoopRun + 1308
9 CoreFoundation 0x2f3e3729 CFRunLoopRunSpecific + 524
10 CoreFoundation 0x2f3e350b CFRunLoopRunInMode + 106
11 GraphicsServices 0x343526d3 GSEventRunModal + 138
12 UIKit 0x31d44871 UIApplicationMain + 1136
13 MyApplication 0x00166417 main (main.m:32)
我做错了什么?
谢谢,
最佳答案
我不能接受这个答案作为书面答案,但现在这个例子可以正常工作,而且我在崩溃报告中没有上述崩溃。
开始吧:
我使用基于包名称的自定义队列 dispatch_get_global_queue
:
let queue = dispatch_queue_create("<BUNDLE_NAME>", nil)
示例
func execute(){
let queue = dispatch_queue_create("<BUNDLE_NAME>", nil)
dispatch_async(queue, {
//Your code to execute in background...
println("Your code to execute in background...")
var transItem:WmTransferItem = self.doInBackground()
dispatch_async(dispatch_get_main_queue(), {
println("code to be executed on the main thread when background task is finished")
self.onPostExecute(transItem)
});
});
}
希望对大家有所帮助。
关于swift - 应用程序在 dispatch_async 上崩溃,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26192651/