22902902
1/13/2020 - 6:24 AM

GCD | 分组 | 顺序执行

// GCD
let group = DispatchGroup()
// 获取下拉数据
let str = [user?.U!,user?.RETCODE! , "", ""]
group.enter()
NetworkTools.share.commodity_query(code: str as Array<Any>) { (result, error) in
            
  if error != nil {
    print("no no no")
    return
  }
            
  // 获取可选类型中的数组
  guard let resultArray = result else {
    print("no")
    return
  }
            
  if let REP = resultArray["REP"] as? [String : AnyObject] {
    if let arr = REP["RESULT"] as? [String : AnyObject]   {
      let code : Int = Int(arr["RETCODE"] as! String)!
        if(Int(truncating: NSNumber(value: code)) < 0) {
          if #available(iOS 13.0, *) {
            self.showMsgbox(_message: "数据加载失败")
            return self.setupVisitorView()
          }
        }
      }
    }
            
            
    if let REP = resultArray["REP"] as? [String : AnyObject] {
      if let resultList = REP["RESULTLIST"] as? [String : AnyObject] {
        for temp in resultList["REC"] as! NSArray {
          let sta = comInfo(dict: temp as! [String : AnyObject])
            self.coi.append(sta.COI!)
            //self.ViewModels.append(sta)           
          }
        }
      }
      group.leave()
    }
    
    //   group.enter() 与 group.leave()  成对出现来控制执行顺序
    group.enter()    
    group.leave()    
    group.notify(queue: DispatchQueue.main) {
      self.psdm.addTarget(self, action: #selector(self.selectDate(_:)), for: .touchDown)
    }