问题
In my app I have a timer that can be up to 5 minutes long. The user can add an element to an array which resets the timer to 5 minutes. When the timer counts town, there's also a UIView which changes its width from 100% of the view to 0% of it in proportion to the timer.
I'd like to run my timer on a background thread to avoid a delay in the timer starting due to other UI events happening.
Is it possible to have a timer run in a background thread whose associated function that the timer calls updates a label or a view width in the UI without threading issues/crashing? So far I haven't been able to get this working.
回答1:
Just need to update your UI from the main thread.
...
dispatch_async(dispatch_get_main_queue(), { () -> Void in
// perform view resizing
})
...
来源:https://stackoverflow.com/questions/43946214/using-a-timer-in-the-background-thread-to-update-ui