我有以下功能,但它一直在崩溃 dispatchGroup.leave() 声明,我不明白为什么。根据我在网上找到的内容,每个 dispatchGroup.leave() 都必须与 dispatchGroup.enter() 相关联,我相信我的函数就是这种情况。
self.kycRecords 仅包含 1 个元素(目前)顺便说一句。
@IBAction func checkCustomerList(_ sender: Any) {
let dispatchGroup = DispatchGroup()
for kycRecord in self.kycRecords {
dispatchGroup.enter()
ApiManager.sharedInstance.postUserToArtemis(kycRecord) {(response, error) in
dispatchGroup.leave()
if error != nil {
kycRecord.kycStatus = "failed"
} else {
if response == true {
kycRecord.kycStatus = "passed"
} else {
kycRecord.kycStatus = "failed"
}
}
}
}
dispatchGroup.notify(queue: DispatchQueue.main, execute: {
print("done")
self.writeOutput()
})
}
它崩溃并显示消息:
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
最佳答案
您可以在离开任何组之前检查组中输入的计数 通过下面的 Patch Work
let count = self.groupExecuting.debugDescription.components(separatedBy: ",").filter({$0.contains("count")}).first!.components(separatedBy: CharacterSet.decimalDigits.inverted).filter({Int($0) != nil})
关于arrays - 离开 DispatchGroup 导致我的代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299012/