简体   繁体   中英

Update UIProgressView progress via delegate

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM