我正在尝试在后台线程中调用我的通知目标。文档声明目标将在通知被排队的地方调用。
如果我将在后台运行的DispatchQueue中的队列包装起来,则永远不会调用目标。如果我删除了DispatchQueue,那么当然会在主线程上调用目标。
for aLocation in locations {
// add it to the queue
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
let notify = Notification(name: MyLocationManager.queueName(), object:aLocation)
NotificationQueue.default.enqueue(notify,
postingStyle: NotificationQueue.PostingStyle.asap,
coalesceMask:NotificationQueue.NotificationCoalescing(rawValue: 0),
forModes: nil)
}
}
答案 0 :(得分:0)
简短的回答是:NotificationQueue
需要run loop
长版:
文档断言,目标将在通知被排队的地方调用。
这不完全正确。 documentation仅针对Notification Center
声明此事实,但不针对NotificationQueue
s
以下是NotificationQueue documentation对此所说的内容:
通知中心在发布时分发通知,而放入队列的通知可以延迟,直到当前通过运行循环结束或直到运行循环空闲。
然后你有enqueue
函数的第四个参数:
<强>模式:强> 可以发布通知的模式列表。如果运行循环处于阵列中提供的某种模式,则通知队列将仅将通知发布到其通知中心。 可能为零,在这种情况下,默认为NSDefaultRunLoopMode 。
将所有这些与GCD
不会在其全局并发队列上创建运行循环的事实相结合,除非某人明确地在其上运行,然后我们在通知队列中有这种行为不开火。