获取主队列块未在主线程上执行

时间:2015-05-05 09:05:11

标签: ios multithreading thread-safety grand-central-dispatch dispatch-async

我调用dispatch_async(dispatch_get_main_queue(),block())而我的block()无法执行UI交互,因为它未在主线程中调用,为什么?

注意:我使用PSPDFUIKitMainThreadGuard

Just a runtume illustration of the issue where a block named showListOfProviders() crash on setting a title of a UIViewController

2 个答案:

答案 0 :(得分:0)

以下是如何在Swift中执行此操作:

runThisInMainThread { () -> Void in
    // Run the method that crashes in here
}

func runThisInMainThread(block: dispatch_block_t) {
    dispatch_async(dispatch_get_main_queue(), block)
}

它作为我的仓库中的标准功能包含在内,请查看:https://github.com/goktugyil/EZSwiftExtensions

答案 1 :(得分:0)

您实际上并没有调度到主线程,从问题的外观来看,您正在调用块并传递块的返回值,例如: dispatch_async(queue, yourReturnValue);

它应该是: dispatch_async(queue, yourBlock); 不: dispatch_async(queue, yourBlock());