Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 562
RodolfoAntonici Avatar asked Aug 30 '26 11:08

RodolfoAntonici


2 Answers

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.

like image 152
Lou Franco Avatar answered Sep 01 '26 02:09

Lou Franco


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()
                }
            }
        }
like image 20
RodolfoAntonici Avatar answered Sep 01 '26 01:09

RodolfoAntonici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!