RodolfoAntonici
RodolfoAntonici

Reputation: 1645

DispatchQueue main isn't called

I have an async task o perform the calculation and the animation, as the animation begins, the queue suspends, when the animation finishes, the queue is resumed. But for some reason the DispatchQueue.main.sync is never called.

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

This is just small calculation, I'll use that to solve tasks more complex than that

Upvotes: 3

Views: 1317

Answers (2)

RodolfoAntonici
RodolfoAntonici

Reputation: 1645

Thanks to Lou Franco's answer I did some changes:

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

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89242

I assume this code is in the main thread.

If so, you have sync'd to a background thread and from there tried to sync back to DispatchQueue.main. But that queue is waiting for the background one to return before it can do anything.

Upvotes: 5

Related Questions