NSDefaultRunLoopMode vs NSRunLoopCommonModes
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When developing iOS or macOS applications using Swift or Objective-C, understanding the nuances of how run loops function is crucial for managing event processing and ensuring smooth application performance. Two terms that often come up in this context are NSDefaultRunLoopMode and NSRunLoopCommonModes. Both pertain to how run loops manage events and input sources, but they serve different purposes and are appropriate for different use cases.
Understanding Run Loops
Before diving into NSDefaultRunLoopMode and NSRunLoopCommonModes, it's important to understand what a run loop is. In essence, a run loop is an event processing loop that your thread enters in order to schedule work and coordinate the receipt of input events. Each thread, including the main thread, has its own run loop.
Key Concepts:
- Modes: Run loops can engage in different modes, which determines the set of events that it will process. Each mode is essentially a named list of input sources to listen for and timers to fire.
- Input Sources: These can be Port-Based, Custom, or Timer sources that the run loop checks during its iterations.
- Observers: These can be attached to run loops to execute code at specific points in the loop’s execution.
NSDefaultRunLoopMode
NSDefaultRunLoopMode is one of the basic run loop modes. When your application starts, the run loop for the main thread begins in this default mode.
Characteristics:
- User Interface Interaction: It handles events such as touches, clicks, and other user interface-related inputs. This is where most of your app’s interaction with the user happens.
- Timer-Driven Tasks: The default mode can also be used for scheduling and managing timer-based events that are needed for general user interactions.
- App's Main Execution: Generally, app-specific code (such as view updates and animations) is run in this mode.
Example Use Case:
In this example, a timer is scheduled to run under the default run loop mode. This means the timer will fire only when the run loop processes events in its default mode (i.e., idle state with no other input processing required).
NSRunLoopCommonModes
NSRunLoopCommonModes is a higher-level abstraction in that it allows grouping multiple run loop modes together. Using NSRunLoopCommonModes, you can ensure that your run loop sources are scheduled in multiple modes without needing to handle each explicitly.
Characteristics:
- Multiple Mode Grouping: This allows your input sources (like timers) to run in more than one specified mode.
- Use Case Flexibility: It supports periods when the run loop is processing user interface tracking modes, like scrolling or other transient UI activities.
Example Use Case:
Consider you want a timer to continue firing even when the user is interacting with the UI (like scrolling a UIScrollView), which would otherwise not be processed under NSDefaultRunLoopMode.
In this example, by adding the timer to NSRunLoopCommonModes, you ensure it remains active even during input events, such as scrolling.
Key Differences
| Feature | NSDefaultRunLoopMode | NSRunLoopCommonModes |
| Role | Default mode for handling main operations | Common mode that includes several modes |
| User Interaction | Yes | Yes (supports multiple modes) |
| Timer Handling | Standard timer management | Continued timer management during UI interactions |
| Use Case | General app operations | Scenarios needing active processing during user actions |
| Mode Inclusion | Single | Multiple modes (e.g., default & UI tracking) |
When to Use Each Mode
- Use
NSDefaultRunLoopModewhen you want your tasks to execute during the app's primary idle state, managing standard interactive UI events. - Use
NSRunLoopCommonModesfor tasks that need to persist through other activity, such as scrolling or user input, where you want timers or sources to remain active.
Additional Considerations
Performance Implications
Using NSRunLoopCommonModes may introduce more processing overhead due to its broader range. Care should be taken not to put too much in the common modes if it can be avoided, as it may degrade performance by actively running sources throughout various modes.
Custom Modes
Creating custom modes is possible if a specific combination of input sources and timer activities is required. However, it generally introduces complexity without much benefit unless you have very particular needs.
In summary, NSDefaultRunLoopMode and NSRunLoopCommonModes are powerful tools for managing an application's run loop, enabling precise control over how and when your application processes input and executes tasks. Understanding these concepts can lead to more responsive and performant applications.
Related reading
- NSDefaultRunLoopMode vs NSRunLoopCommonModes
- NSDictionary with ordered keys
- NSIndexpath.item vs NSIndexpath.row
- NSInvalidUnarchiveOperationException Could not instantiate class named NSLayoutConstraint
- NSLayoutConstraint crashes ViewController
- NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?
- NSNotificationCenter addObserver in Swift
- NSNotificationCenter addObserver in Swift
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.