NSDefaultRunLoopMode
NSRunLoopCommonModes
iOS development
RunLoop modes
Apple programming

NSDefaultRunLoopMode vs NSRunLoopCommonModes

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

objc
1NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
2                                                  target:self
3                                                selector:@selector(updateDisplay)
4                                                userInfo:nil
5                                                 repeats:YES];

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.

objc
1NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
2                                         target:self
3                                       selector:@selector(updateDisplay)
4                                       userInfo:nil
5                                        repeats:YES];
6[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

In this example, by adding the timer to NSRunLoopCommonModes, you ensure it remains active even during input events, such as scrolling.

Key Differences

FeatureNSDefaultRunLoopModeNSRunLoopCommonModes
RoleDefault mode for handling main operationsCommon mode that includes several modes
User InteractionYesYes (supports multiple modes)
Timer HandlingStandard timer managementContinued timer management during UI interactions
Use CaseGeneral app operationsScenarios needing active processing during user actions
Mode InclusionSingleMultiple modes (e.g., default & UI tracking)

When to Use Each Mode

  • Use NSDefaultRunLoopMode when you want your tasks to execute during the app's primary idle state, managing standard interactive UI events.
  • Use NSRunLoopCommonModes for 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.