Indeterminate progress indicator not animating

1.1k Views Asked by At

The progress indicator does not start animating even if I use queues or sleep.

I have a function that reads contents of a directory and for each mp3 file it gets the file tags. Also, I read posts about progress indicator but nothing helped me so far.

{
    progressIndicator.startAnimation(self)
    sleep(2)
        if let item = self.outlineView.item(atRow: self.outlineView.selectedRow)  {
             self.arrMp3File.removeAll()
            do {
                let xx = (item as! DirectoryItem).url
                let b = self.getSubDir(path: xx.path)
                print(xx.path)
                for bb in b {
                    self.getTagsFromFile(file: xx.path+"/"+bb)
                }
            }
            self.arrMp3File.sort(by: {
                $0.file < $1.file
            })
            self.loadTagsFromButton.isEnabled = true
            self.tableView.reloadData()
            }
    self.progressIndicator.stopAnimation(self)
}

I would like to show the animation, so the user knows the app is not frozen. If I remove stopAnimation it starts animating after the files are shown on the tableview.

I added that sleep just to check the animation.

1

There are 1 best solutions below

0
Renan Aguiar On BEST ANSWER

I solved it.

{
    if let item = self.outlineView.item(atRow: self.outlineView.selectedRow)  {
        self.arrMp3File.removeAll()
        do {
            let xx = (item as! DirectoryItem).url
            let b = self.getSubDir(path: xx.path)
            DispatchQueue.global(qos: .background).async { [weak self] in
                DispatchQueue.main.async {
                    self?.progressIndicator.isHidden = false
                    self?.progressIndicator.startAnimation(self)
                }
                for bb in b {
                    self?.getTagsFromFile(file: xx.path+"/"+bb)
                }
                DispatchQueue.main.async {
                    self?.arrMp3File.sort(by: {
                        $0.file < $1.file
                    })
                    self?.tableView.reloadData()
                    self?.loadTagsFromButton.isEnabled = true
                    self?.progressIndicator.stopAnimation(self)
                    self?.progressIndicator.isHidden = true
                }
            }
        }
    }   

}