美文网首页
DispatchSemaphore & DispatchGrou

DispatchSemaphore & DispatchGrou

作者: Kare | 来源:发表于2019-05-10 16:37 被阅读0次

    标记

    DispatchSemaphore

    let semaphore = DispatchSemaphore(value: 0)
        let dispatchQueue = DispatchQueue.global()
        dispatchQueue.async {
            WKManager().requestPreiodTrendData(symbol: self.compare1Item.code) { obj in
                if let obj = obj {
                    self.trend1Data = obj.sorted(by: { (obj1, obj2) -> Bool in
                        return obj1.key < obj2.key
                    })
                    semaphore.signal()
                }
            }
            semaphore.wait()
            
            WKManager().requestPreiodTrendData(symbol: self.compare2Item.code) { obj in
                if let obj = obj {
                    self.trend2Data = obj.sorted(by: { (obj1, obj2) -> Bool in
                        return obj1.key < obj2.key
                    })
                    semaphore.signal()
                }
            }
            semaphore.wait()
            
            DispatchQueue.main.async {
                if self.indicatorView.isAnimating {
                    self.indicatorView.stopAnimating()
                }
                self.loadDataAndShow()
            }
        }
    

    DispatchGroup

    let group = DispatchGroup()
        group.enter()
        WKManager().requestPreiodTrendData(symbol: self.compare1Item.code) { obj in
            if let obj = obj {
                self.trend1Data = obj.sorted(by: { (obj1, obj2) -> Bool in
                    return obj1.key < obj2.key
                })
                group.leave()
            }
        }
    
        group.enter()
        WKManager().requestPreiodTrendData(symbol: self.compare2Item.code) { obj in
            if let obj = obj {
                self.trend2Data = obj.sorted(by: { (obj1, obj2) -> Bool in
                    return obj1.key < obj2.key
                })
                group.leave()
            }
        }
    
        group.notify(queue: .main) {
            if self.indicatorView.isAnimating {
                self.indicatorView.stopAnimating()
            }
            self.loadDataAndShow()
        }
    

    相关文章

      网友评论

          本文标题:DispatchSemaphore & DispatchGrou

          本文链接:https://www.haomeiwen.com/subject/phnaoqtx.html