Mobile Development 24 min read

Understanding CADisplayLink, NSTimer, and RunLoop in iOS: Implementation, Issues, and Solutions

This article explains the principles, usage, and internal mechanisms of CADisplayLink and NSTimer in iOS, compares their advantages and drawbacks, discusses common pitfalls such as retain cycles, explores RunLoop modes and sources, and provides code examples and solutions for reliable timer handling.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding CADisplayLink, NSTimer, and RunLoop in iOS: Implementation, Issues, and Solutions

The article provides a comprehensive overview of iOS timing mechanisms, focusing on CADisplayLink and NSTimer , their properties such as timestamp , duration , preferredFramesPerSecond , and how they interact with the system's display refresh cycle.

It demonstrates how to create, configure, and use these timers in a view controller, including code snippets for initializing a CADisplayLink , adding it to a run loop, handling pause/resume, and calculating actual frame rates.

The article also discusses common problems like retain cycles when timers strongly reference view controllers, and presents a proxy‑based solution using an NSProxy subclass to break the strong reference chain.

Further, it delves into the RunLoop architecture, explaining the roles of CFRunLoopSourceRef , CFRunLoopTimerRef , and CFRunLoopObserverRef , the concept of RunLoop modes (e.g., kCFRunLoopDefaultMode , UITrackingRunLoopMode ), and how timers and display links are scheduled within these modes.

Differences between iOS 14 and iOS 15 implementations of CADisplayLink are examined, showing the shift from a VSync‑driven source to a UIKit‑driven source and the impact on callback behavior.

Finally, the article offers a practical fix for monitoring dynamic refresh rates on ProMotion devices by reading the duration property and adjusting preferredFrameRateRange accordingly, along with references to official documentation and further reading.

mobile developmentiOSRunLoopTimerCADisplayLinkNSTimer
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.