DispatchQueue主要没有被调用

时间:2017-10-18 19:45:29

标签: ios swift grand-central-dispatch

我有一个异步任务o执行计算和动画,当动画开始时,队列暂停,当动画结束时,队列恢复。但由于某种原因,永远不会调用DispatchQueue.main.sync。

self.someQueue = DispatchQueue(label: "Animation", qos: .background, attributes: DispatchQueue.Attributes.concurrent)
    for index in stride(from: volumeObjects.count, to: -1, by: -1) {
        mainAsyncQueue.sync{
            self.dynamicViewModel.currentPercentile = Float(index)/Float(volumeObjects.count)
            self.currentObjectIndex = index
            print(index)

            DispatchQueue.main.sync {
                UIView.animate(withDuration: 1, animations: {
                    self.updateViewModelDynamicViewModel()
                }, completion: { (finished) in
                    self.someQueue.resume()
                })
            }
            self.someQueue.suspend()
        }
    }

这只是一个小计算,我会用它来解决比那个更复杂的任务

2 个答案:

答案 0 :(得分:4)

我假设这段代码在主线程中。

如果是这样,您已同步到后台线程,并从那里尝试同步回DispatchQueue.main。但是,该队列正在等待后台返回,然后才能执行任何操作。

答案 1 :(得分:1)

感谢Lou Franco的回答,我做了一些改变:

DispatchQueue.global().async {
            self.someQueue = DispatchQueue(label: "Animation", qos: .background, attributes: DispatchQueue.Attributes.concurrent)
            for index in stride(from: volumeObjects.count, to: -1, by: -1) {
                self.someQueue.sync{
                    self.dynamicViewModel.currentPercentile = Float(index)/Float(volumeObjects.count)
                    self.currentObjectIndex = index
                    print(index)

                    DispatchQueue.main.sync {
                        UIView.animate(withDuration: 0.1, animations: {
                            self.updateViewModelDynamicViewModel()
                        }, completion: { (finished) in
                            if finished {
                                self.someQueue.resume()
                            }
                        })
                    }
                    self.someQueue.suspend()
                }
            }
        }