代码之家  ›  专栏  ›  技术社区  ›  Rutger Huijsmans

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

  •  2
  • Rutger Huijsmans  · 技术社区  · 6 年前

    我有以下功能,但它总是在 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_指令(代码=exc_i386_invop,子代码=0x0)

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   Inder Manali    5 年前

    您可以在离开任何组之前检查组中输入的计数数 按下面 修补工程

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