从另一个 GCD 队列中捕获 NSNotification

Catch NSNotification from another GCD queue

我是 运行 使用此代码的后台进程:

func launchTor(hashedPassword hash : String) {
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
    // Do some stuff here
    let observer = NSNotificationCenter.defaultCenter().addObserverForName("AppTerminates", object: nil, queue: nil) {
            notification -> Void in
            print("Terminating...")
            // Do smh here
        }
    }
    // Just some more stuff
}

有没有办法指定需要捕获通知的队列(GCD)?

- addObserverForName:object:queue:usingBlock: method is an extra feature provided by NSOperationQueue class 基于 Grand Central Dispatcher:

Operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations.

(从NSOperationQueue class参考开始)

如果有原因(例如不同的行为或教育目的),可以重新实现此功能,否则可能值得切换到 NSOperationQueue 而不是裸 GCD。