我知道这个主题已经有很多贡献。我尝试了DispatchGroup的不同变体,但似乎我无法在完成某个任务之前使整个循环停止。
let names = ["peter", "susan", "john", "peter", "susan", "john"]
var holding = [String: [Double]]()
for i in 0...10 {
for name in names {
if holding[name] == nil {
Alamofire.request("https://jsonplaceholder.typicode.com", parameters: parameters).responseJSON { responseData in
// do stuff here
holding[name] = result
}
} else {
// do other stuff with existing "holding[name]"
}
// if if holding[name] == nil, the whole process should wait
}
}
如果我使用DispatchGroup,Alamofire请求将逐个执行,但整个循环无法识别holding[name]
是否已存在。因此holding[name]
始终为nil
,因为循环不会等待。
非常感谢!
编辑:
根据Mikes和Versus的回答,我尝试了以下内容:
var names = ["peter", "susan", "john", "peter", "susan", "john"]
var holding = [String: [Double]]()
let semaphore = DispatchSemaphore(value: 1)
for i in 0...10 {
DispatchQueue.global().async { [unowned self] in
self.semaphore.wait()
for name in names {
if holding[name] != nil {
Alamofire.request("https://jsonplaceholder.typicode.com", parameters: parameters).responseJSON { responseData in
// do stuff here
holding[name] = result
semaphore.signal()
}
} else {
// do other stuff with existing "holding[name]"
semaphore.signal()
}
// if if holding[name] != nil, the wholeprocess should wait
}
}
}
但不幸的是应用程序崩溃了。我做错了什么?
答案 0 :(得分:2)
你有两个选择
1)Semaphore
2)Operation Queues
但在使用Semaphores
您需要对semaphore.signal()
和semaphore.wait()
对
由于Semaphore
可能阻止主线程所以所有操作都应在Dispatch.global.async
<强>即强>
semaphore.wait()
Alamofire.request("https://jsonplaceholder.typicode.com", parameters: parameters).responseJSON { responseData in
semaphore.signal()
holding[name] = result
}
这里阻止主线程
问题是完成处理程序在主线程中执行,主线程已被最初调用的semaphore.wait()锁定。因此,当完成发生时,semaphore.signal()永远不会被调用
你必须使用
DispatchQueue.global().async { [unowned self] in
self.semaphore.wait()
// And other statemetns
}
希望它对您和其他人有帮助
答案 1 :(得分:1)
我认为您可以使用DispatchSemaphore来停止循环:
let semaphore = DispatchSemaphore(value: 1)
for i in 0...10 {
for name in names {
// if signal is 0, loop will be stopped forever,
// if signal > 0, loop will continue
semaphore.wait()
if holding[name] == nil {
Alamofire.request("https://jsonplaceholder.typicode.com", parameters: parameters).responseJSON { responseData in
// do stuff here
// release signal to continue current loop(signal + 1)
semaphore.signal()
holding[name] = result
}
} else {
// do other stuff with existing "holding[name]"
}
// if if holding[name] == nil, the whole process should wait
}
}