DispatchSemaphore & DispatchGroup

标记

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 & DispatchGroup)