ios - UIProgressView won't update progress when updated from a dispatch -


i'm trying make progress bar act timer , count down 15 seconds, here's code:

private var timer: dispatch_source_t! private var timeremaining: double = 15  override public func viewdidappear(animated: bool) {     super.viewdidappear(animated)     profilepicture.layer.cornerradius = profilepicture.bounds.width / 2      let queue = dispatch_queue_create("buzz.qualify.client.timer", nil)     timer = dispatch_source_create(dispatch_source_type_timer, 0, 0, queue)     dispatch_source_set_timer(timer, dispatch_time_now, 10 * nsec_per_msec, 5 * nsec_per_msec)     dispatch_source_set_event_handler(timer) {         self.timeremaining -= 0.01;         self.timerbar.setprogress(float(self.timeremaining) / 15.0, animated: true)         print(string(self.timerbar.progress))     }     dispatch_resume(timer) } 

the print() prints proper result, progress bar never updates, somestimes single update @ around 12-15% full , jump there , nothing else.

how can make bar steadily flow down, , execute task @ end of timer without blocking ui thread.

in siburb's answer, correctly points out should make sure ui updates happen on main thread.

but have secondary observation, namely you're doing 100 updates per second, , there's no point in doing fast because maximum screen refresh rate 60 frames per second.

however, display link timer, except it's linked screen refresh rate. like:

var displaylink: cadisplaylink? var starttime: cfabsolutetime? let duration = 15.0  func startdisplaylink() {     starttime = cfabsolutetimegetcurrent()     displaylink = cadisplaylink(target: self, selector: "handledisplaylink:")     displaylink?.addtorunloop(nsrunloop.mainrunloop(), formode: nsrunloopcommonmodes) }  func stopdisplaylink() {     displaylink?.invalidate()     displaylink = nil }  func handledisplaylink(displaylink: cadisplaylink) {     let percentcomplete = float((cfabsolutetimegetcurrent() - starttime!) / duration)     if percentcomplete < 1.0 {         self.timerbar.setprogress(1.0 - percentcomplete, animated: false)     } else {         stopdisplaylink()         self.timerbar.setprogress(0.0, animated: false)     } }  override func viewdidappear(animated: bool) {     super.viewdidappear(animated)      startdisplaylink() } 

alternatively, if you're doing on background thread wants post updates uiprogressview faster main thread can service them, i'd post main thread using dispatch source of type dispatch_source_type_data_add.

but, if you're trying update progress view on fixed period of time, display link might better timer.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -