2017-10-18 44 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() 
       } 
      } 
     } 
相關問題