离开 DispatchGroup 会导致我的代码崩溃

Rut*_*ans 10 arrays grand-central-dispatch swift

我有以下函数,但它在 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()
        })
    }
Run Code Online (Sandbox Code Playgroud)

它崩溃并显示以下消息:

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

在此处输入图片说明

小智 7

在离开任何小组之前,您可以通过下面的补丁工作检查在小组中输入的计数数量

let count = self.groupExecuting.debugDescription.components(separatedBy: ",").filter({$0.contains("count")}).first!.components(separatedBy: CharacterSet.decimalDigits.inverted).filter({Int($0) != nil})
Run Code Online (Sandbox Code Playgroud)