2017-10-18 116 views
2

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

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

这仅仅是计算量小,我就用它来解决的任务比

+0

我看不出有任何* *异步任务。 'mainAsyncQueue'实际上是'backgroundSyncQueue';) – vadian

+1

好的变量名纳粹:P改变它 – RodolfoAntonici

回答

3

我认为这个代码是在主线程中更复杂。

如果是这样,您已同步到后台线程,并从那里尝试同步回DispatchQueue.main。但是该队列正在等待后台在返回任何事情之前返回。

0

感谢娄佛朗哥的答案,我做了一些变化:

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