主队列未被调用。

3

我有一个异步任务需要计算和动画,当动画开始时,队列暂停,当动画结束时,队列恢复。但出于某种原因,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()
        }
    }

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


我没有看到任何async任务。mainAsyncQueue实际上是backgroundSyncQueue ;) - vadian
1
变量名字改一下吧,不要用"Nazi"这个词。 - RodolfoAntonici
2个回答

5
我假设这段代码在主线程中。
如果是这样,你已经同步到了一个后台线程,然后尝试再次同步回DispatchQueue.main。但是该队列正在等待后台线程返回后才能执行任何操作。

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()
                }
            }
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接