离开 DispatchGroup 导致我的代码崩溃

Leaving DispatchGroup causes my code to crash

我有以下功能,但它总是崩溃 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)

您可以在离开任何组之前查看进入组的计数 通过下面 补丁工作

let count = self.groupExecuting.debugDescription.components(separatedBy: ",").filter({[=10=].contains("count")}).first!.components(separatedBy: CharacterSet.decimalDigits.inverted).filter({Int([=10=]) != nil})