2

So I have this weird issue with a UIProgressView. I run a background task via Alamofire fetching some objects through a custom api. Each time a group of objects gets fetched I want to show that through that progressView. However, the progress indicator does not move along the assigned progress but only as soon as the entire progress is finished it just jumps to 100% in one go. The progress is transferred from the background from another class via delegate and its been displayed correctly in log prints. BUT the progressView is not moving alongside.

It gets triggered by:

delegate?.updateProgressF?(progressText: "Some Text", progress: 0.1)

And the Function:

func updateProgressF(progressText: String, progress: CGFloat) {
    
    let date = Date()
    let calendar = Calendar.current
    let seconds = calendar.component(.second, from: date)
    let nanoSeconds = calendar.component(.nanosecond, from: date)
    print(progress, seconds, nanoSeconds, progressView.progress)

    DispatchQueue.main.async { [self] in
        progressView.setProgress(Float(progress), animated: true)
    }

}

It prints:

0.1 23 123411059 0.0
0.2 28 800351977 0.0
0.4 28 804774999 0.0
0.6 28 806638002 0.0
0.8 28 885470032 0.0
0.9 32 190487980 0.0
1.0 32 253463029 0.9

Any advice is much appreciated

snksnk
  • 1,566
  • 4
  • 21
  • 46
  • Can you elaborate a bit about the usage of background tasks here? You are running some operations in the background, fetching data and updating the application UI while you are in the background? And when you are back to the foreground the progress view is full/as full as it get to in the progress? Or what are you trying to update a UI of an extension/widget? – CloudBalancing Jan 16 '22 at 12:53
  • @CloudBalancing thank you for the reply. In the background am fetching and sorting json files from api. In the process of sorting and "cleaning" this info (which I do in Steps) I want to show this progressView.. So I delegate at the beginning of each step so the user can see the progress.. I try to update the UI in the main thread but still no luck.. – snksnk Jan 16 '22 at 14:20
  • You can not update your Ui while your app is in the background – Leo Dabus Jan 16 '22 at 15:16
  • Btw you should not update your progress view manually. Get your data task progress and set the progress view observedProgress https://stackoverflow.com/a/67309732/2303865 – Leo Dabus Jan 16 '22 at 15:18
  • @LeoDabus I do not update the UI while the app is in the background.. I want to update it while it does some background tasks. The observed progress does not help me as I want to display the progress in steps while the JSON is sorted passing through some custom functions – snksnk Jan 17 '22 at 12:41
  • Anyway you can’t update the UI while the app is in the background. Not sure what you mean by while it does some background tasks. – Leo Dabus Jan 17 '22 at 12:48
  • You are not showing enough info in your post on how you are calling the delegate method. – Leo Dabus Jan 17 '22 at 12:50

1 Answers1

0

I figured it out. So for anyone in the future with similar issue just make sure all the tasks are running in the background then call the progressView on the main thread. In my case I had to specify in my viewDidLoad:

DispatchQueue.global(qos: .background).async {[self] in
    fetchInfo()
}

Then call the UI change on the main thread:

DispatchQueue.main.async { [self] in
    progressView.setProgress(Float(progress), animated: true)
}
snksnk
  • 1,566
  • 4
  • 21
  • 46