Setting UIProgressView from asynchronous Background Tasks

172 views Asked by At

I'm new to Swift and build a small App which transfers some HealthKit Data to a self-build webservice. I placed a UIProgressView in my Main.storyboard and connected it to ViewController.swift:

@IBOutlet weak var syncHealthProgress: UIProgressView!

From within an action there are 6 calls to a webservice via multiple steps outside the ViewController. First I fetch the Health data which is done in background (HKStatisticsCollectionQuery). Within the background task I take the result and make a API call with the data (URLSession) which is done asynchronous too.

What I want to do: everytime the API call succeed I want to raise the progress by 1/6.

So what is the best practise for this? It is not possible to access the UI from outside of the main thread (tried to transfer the syncHealthProgress as parameter down to the API calls which looks dirty and doesn't worked).

Any hint?

1

There are 1 answers

0
nicksweet On BEST ANSWER

If your just trying to ensure your UI update logic runs on the main thread then just do:

DispatchQueue.main.async {
    // code to run on main queue
}