等待任务完成
如何让代码等待,直到DispatchQueue中的任务结束?它是否需要任何CompletionHandler或什么?等待任务完成
func myFunction() {
var a: Int?
DispatchQueue.main.async {
var b: Int = 3
a = b
}
// wait until the task finishes, then print
print(a) // - this will contain nil, of course, because it
// will execute before the code above
}
我使用的Xcode 8.2和写入斯威夫特3.
使用DispatchGroup
s到实现这一目标。您可以得到通知时,该集团的enter()
和leave()
电话是平衡的:
func myFunction() {
var a: Int?
let group = DispatchGroup()
group.enter()
DispatchQueue.main.async {
a = 1
group.leave()
}
// does not wait. But the code in notify() gets run
// after enter() and leave() calls are balanced
group.notify(queue: .main) {
print(a)
}
}
,或者你可以等待(和返回):
func myFunction() -> Int? {
var a: Int?
let group = DispatchGroup()
group.enter()
// avoid deadlocks by not using .main queue here
DispatchQueue.global(attributes: .qosDefault).async {
a = 1
group.leave()
}
// wait ...
group.wait()
// ... and return as soon as "a" has a value
return a
}
注意:group.wait()
块当前队列(可能是主队列在你的情况),所以你必须在另一个队列dispatch.async(如上面的示例代码),以避免的死锁。
使用派遣组
dispatchGroup.enter()
FirstOperation(completion: { _ in
dispatchGroup.leave()
})
dispatchGroup.enter()
SecondOperation(completion: { _ in
dispatchGroup.leave()
})
dispatchGroup.wait() //Waits here on this thread until the two operations complete executing.
假设你把这个主队列中,这会导致死锁。 – shallowThought
@shallowThought如此真实。 – Prateekro
我想在另一个类中执行一个函数,但我想等待完成该函数,然后在当前类中继续如何处理该函数? –
在Swift 3中,当DispatchQueue
完成一项任务时,不需要完成处理程序。 此外,你可以用不同的方式实现你的目标
一种方法是这样的。
var a: Int?
let queue = DispatchQueue(label: "com.app.queue")
queue.sync {
for i in 0..<10 {
print("Ⓜ️" , i)
a = i
}
}
print("After Queue \(a)")
它将等到循环结束,但在这种情况下,您的邮件线程将被阻止。
你也可以做同样的事情,这样的
let myGroup = DispatchGroup()
myGroup.enter()
//// Do your task
myGroup.leave() //// When your task completes
myGroup.notify(queue: DispatchQueue.main) {
////// do your remaining work
}
最后一件事。如果要在使用DispatchQueue完成任务时使用completionHandler,则可以使用DispatchWorkItem
。
下面是一个例子,如何使用DispatchWorkItem
let workItem = DispatchWorkItem {
// Do something
}
let queue = DispatchQueue.global()
queue.async {
workItem.perform()
}
workItem.notify(queue: DispatchQueue.main) {
// Here you can notify you Main thread
}
我想在另一个类中执行一个函数,但我想等待完成该函数,然后在当前类中继续如何处理该函数? –
@SaeedRahmatolahi:或者使用'wait'方法(如果你没有问题阻止,即如果你不在主线程中),或者提供一个完成处理程序或在你的调用类中使用notify方法。 – shallowThought
为什么你要在异步块外调用'group.enter'?每个区块都有责任进入和离开组织吗? – Bill