美文网首页
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

    标记 DispatchSemaphore DispatchGroup

  • 多线程

    异步网络请求串行 DispatchSemaphore初始值为1 DispatchSemaphore初始值为0还是1...

  • DispatchSemaphore

    /// 信号量对象class DispatchSemaphore { }参考: https://blog.csdn...

  • Swift DispatchSemaphore

    信号量机制实质是PV操作,具体操作如下: 1、P操作的动作是: (1)S减1; (2)若S减1后大于或等于零,则程...

  • 【转】DispatchSemaphore

    ————————————————版权声明:本文为CSDN博主「vision66」的原创文章,遵循 CC 4.0 B...

  • iOS 信号量semaphore学习总结

    常用API: let semaphore = DispatchSemaphore.init(value: 1)//...

  • Swift GCD DispatchSemaphore

    信号量处理事件并发 用来控制资源被多任务访问的情况 亲测有效 延迟方法 日志输出

  • ios gcd实现多图上传,控制并发数

    DispatchSemaphore&DispatchGroup实现多图上传并控制并发数,并且实现线程同步 需求:实...

  • Dispatch

    1. 定时执行 2. DispatchSemaphore信号量 3. concurrent并发 并发的快速发起: ...

  • 简单易懂的DispatchSemaphore

    DispatchSemaphore 信号量,一种用来控制并发访问资源的机制,多用于多线程中,可以控制并发线程数量。...

网友评论

      本文标题:DispatchSemaphore & DispatchGrou

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