Run loop mode is a set of input sources and timers for monitoring and a set of run loop observers for notification. Each time you run a run loop, you specify (explicitly or implicitly) the run mode of the run loop. When the corresponding mode is passed to the run loop, only the input sources corresponding to the mode are monitored and allow the run loop to process the event (similar to this, only the observers corresponding to this mode will be notified ). Events that are different from the specified mode are saved by the event source and distributed only after the relevant mode is registered.
In your code, the mode is identified by a name. Both cocoa and core Foundation define the default mode and several common modes and use strings to identify them. You can use custom strings to customize the mode. Although you can specify any name for the custom mode, the content contained in the mode is fixed. That is, you must add one or more input sources, timers, or run-loop observers for the mode.
You use mode to filter the events passed by run loop in unwanted sources. In most cases, you want your run loop to run in the default mode defined by the system. A modal Panel, however, might run in the "Modal" mode. While in this mode, only sources relevant to the modal panel wocould
Deliver events to the thread. For secondary threads, you might use custom modes to prevent low-priority sources from delivering events during time-critical operations.
Note: mode only distinguishes event sources, rather than event types. For example, you cannot use mode to match the cursor button event or keyboard event. You can use mode to listen to a set of different ports so that timer temporarily suspends or changes the sources or run-loop observers currently monitored.
The following table describes the modes and brief descriptions defined in cocoa and core Foundation. The name column is the mode constant you specified in the Code:
| Mode |
Name |
Description |
| Default |
Nsdefaultrunloopmode
(Cocoa)
Kcfrunloopdefaultmode
(Core Foundation) |
The default mode is used in most operations. Most of the time, you use this mode to start the run loop and configure your input sources. |
| Connection |
Nsconnectionreplymode
(Cocoa) |
Cocoa uses this mode to connect to the nsconnection object for monitoring the response. You usually do not need to use this mode in your code. |
| Modal |
Nsmodalpanelrunloopmode
(Cocoa) |
Cocoa uses this mode to identify events used for modal panel. |
| Event tracking |
Nseventtracking-runloopmode (cocoa) |
Cocoa uses this mode to restrict the events reported by the cursor dragging loop or trace loops related to other user interfaces. |
| Common Modes |
Nsunloopcommonmodes
(Cocoa)
Kcfrunloopcommonmodes
(Core Foundation) |
This is a set of configurable general patterns. If you associate input sources with this mode, the input sources is also associated with other modes in this group. For cocoa applications, the default mode includes the default, modal, and event tracking modes. Core Foundation only contains the default mode during initialization. You can use cfrunloopaddcommonmode to add a custom mode for this mode. |
Input sources
Input sources sends the event to your thread asynchronously. The event source depends on the input sources type, which is usually divided into one or two groups. Port-based event sources monitor the ports of your machine and customize custom sources for Input Source Monitoring events. Run loop does not care whether input sources is a port-based or custom event source. The only difference between two different input sources is their identification method. The Port-based input source ID is automatically generated by the kernel, and the custom Input Source is manually generated by another thread.
When you create an input sources, specify it to your run loop and assign one or more modes. When mode is assigned, the monitoring of input source will be immediately affected. In most cases, you can run your run loop in the default mode, and you can also specify the custom mode. If an input sources is not in the currently monitored mode, any events generated by this sources will be saved until the run loop runs in the corresponding mode.
Port-based sources
Cocoa and core Foundation provide built-in port-based sources creation using port-related objects and functions. For example, in cocoa, you never need to directly create input sources. You only need to use the port object and the nsport method to add the port to the run loop. The port object will create and configure input sources for you.
At core Foundation, you need to create the port and run loop input sources by yourself. In this case, you will use the function (cfmachportref, cfmessageportref, cfsocketref) related to the port opacity type to create an appropriate object.
Custom Input Source
To create a custom input source, you must use a function related to the cfrunloopsourceref opacity type. You will use several callback functions to configure custom input sources. Core Foundation calls these callback functions at different times to complete source configuration, process events, and disable the source when the source is removed from the run loop.
In addition to defining the behaviors of custom input sources when an event arrives, you must also define the event shipping mechanism. This part of the source runs on a separate thread and is responsible for providing the Input Source with its data and for signaling it when that data is ready
Processing. The event Delivery Mechanic is up to you but need not be overly complex.
Cocoa perform selector Source
In addition to Port-based sources, cocoa defines a set of custom input sources for executing a method (selector) in any thread ). Similar to the port-based source, the perform selector is serialized and executed in the thread, which mitigates the synchronization problems caused by running multiple methods in the same thread. Unlike port-based sources, perform selector source automatically runs from
Loop.
When the perform selector is not in the main thread, the thread must have an active run loop. For a self-created thread, the perform selector can be executed only after your Code explicitly runs a run loop. Run loop processes all queued perform selectors while the loop is running, instead of processing only one perform selector during a loop.
The following table shows how to call the perform selector:
| Methods |
Description |
Performselectoronmainthread: withobject: waituntildone:
Performselecstmmainthread: withobject: waituntildone: modes: |
The specified selector is called in the next run loop of the main thread of the application. These methods provide you with blocking the current thread execution until the selector execution is complete. |
Performselector: onthread: withobject: waituntildone:
Performselector: onthread: withobject: waituntildone: modes: |
Call the specified selector in an existing thread. These methods provide you with blocking the current thread execution until the selector execution is complete. |
Optional mselector: withobject: afterdelay:
Optional mselector: withobject: afterdelay: inmodes: |
In the next run loop of the current thread and delay an optional time, call the specified selector. |
Cancelpreviousperformrequestswithtarget:
Cancelpreviousperformrequestswithtarget: selector: object: |
Cancels the message sent to the thread using the method in the third line. |
When to use run loop?
You must run the run loop explicitly only when you create the second thread. The run loop of main thread is part of the framework. Therefore, the cocoa and carbon frameworks provide code that automatically runs the main loop of the application. In iOS, the run method of uiapplication is used to start the main loop of the application and serve as part of the startup sequence. If you use xcode template to generate an application, you do not need to explicitly call the run method.
For the second thread, You need to determine whether it is necessary to use the run loop. If necessary, You need to manually configure and start it. You do not need to create a run loop for each newly created thread. For example, if you use your thread to run tasks that run for a long time and have predictable results, you can not start the thread's run loop. The problem solved by run loop is that you need to have a lot of interaction with the thread.
If you encounter the following situations, you need to start the run loop in the thread:
Use ports or custom input sources to interact with other threads;
Use timers (timer) in thread );
Use any javasmselector... method in the cocoa application;
Use thread to complete periodic tasks.
If you decide to use run loop, the configuration and operation will be very direct. Just like all multi-thread programming, you will consider the conditions for suspending new threads. The new thread is always safely exited before forced exit.