应用程序在dispatch_async,Swift上崩溃

时间:2014-10-04 12:06:54

标签: swift

我有以下代码段,有时此行: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
                });
            });            
    }

这是什么意思:with unmangled suffix "_promote0"

例外:

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)

我做错了什么?

谢谢,

1 个答案:

答案 0 :(得分:0)

我不能接受这个答案作为回答,但是现在这个例子工作正常,我在崩溃报告中没有上面提到的崩溃。

我们走了:

我使用基于包名称的自定义队列而不是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)
            });
        });

}

希望它会对某人有所帮助。