UIScrollView pauses NSTimer until scrolling finishes
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding UIScrollView Pausing NSTimer
UIScrollView is a fundamental component in iOS development that allows for the creation of scrollable content views. It is commonly used for displaying large content, like text or images, that does not fit in a single screen. However, one peculiar behavior developers often encounter is that UIScrollView can pause NSTimer events during active scrolling. This article delves into the technical specifics, causes, and potential solutions for this.
The Mechanics of UIScrollView and NSTimer Interaction
In iOS, `NSTimer` is a utility for scheduling timer-based code execution. It is commonly employed for repeating tasks or delayed task execution within applications. While NSTimer can function seamlessly with UI components, complications arise when it interacts with UIScrollView.
The Issue
When a user initiates a scroll action on a `UIScrollView`, the system engages a default behavior that can inadvertently pause NSTimer instances. This behavior occurs because:
- Run Loop Mode: NSTimer is associated with a particular run loop mode, usually `NSDefaultRunLoopMode`. When UIScrollView is actively scrolling, it switches the run loop mode to `UITrackingRunLoopMode`. As a result, timers scheduled with `NSDefaultRunLoopMode` are temporarily suspended, affecting tasks dependent on precise timing.
Technical Explanation
Here's a typical flow that illustrates how UIScrollView pausing impacts NSTimer:
- Initialization: You set up an `NSTimer` to fire every second while using a label to show a ticking count.
- The run loop enters `UITrackingRunLoopMode`.
- As the run loop mode switches, the NSTimer doesn't trigger `updateCounter` until scrolling ceases.
- Performance: Keep in mind that by running NSTimer during intensive interactions, like scrolls, app performance could degrade. Ensure your timer-based tasks are lightweight to avoid frame drops or similar issues.
- CADisplayLink: As an alternative to NSTimer, you could use `CADisplayLink`, which is more closely aligned with the display refresh cycle and doesn't suffer from run loop mode issues.
- GCD Timers: For tasks requiring precise timing and reduced overhead, consider using Grand Central Dispatch (GCD) timers.
Related reading
- Unable to create new native thread error - but very few threads are in use
- Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint
- Unbalanced data and weighted cross entropy
- Under what conditions do these non-comparison sorts run in linear time?
- UIScrollView scroll to bottom programmatically
- UIScrollView Scrollable Content Size Ambiguity
- Understand Kafka write speed
- Understanding memory usage for kubectl top node

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.