The following are all from the Internet, but it is a summary of the individual extracts, the main analysis of "only when the message loop pattern matches the event pattern, the message loop will run" This sentence pattern matching, which patterns match which patterns.
Each thread has a message loop, the main thread message loop is turned on by default, and the child thread message loop is turned off by default and needs to be turned on manually. The relationship between the message loop and the thread is one by one, and its relationships are stored in a global dictionary (the Key for the dictionary is the thread, and value is the message loop).
You can only destroy the message loop at the end of the thread. In addition to the main thread, the UI thread, you can only get the message loop inside the thread.
To add an event to a message loop, there are two types of events:
- Input Source: An asynchronous delivery event, typically a message from a different thread or from a different app. The input source asynchronously passes an event to the corresponding handler and the Nsrunloop object of the thread association Rununtildate: method to exit event handling.
- Timing Source: Synchronously passes events, which occur at each timer call or periodically called. The timer source passes events to their handlers, but does not call run loop to exit processing.
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
[Self demo];
}
-(void) demo{
nstimer* timer = [Nstimer timerwithtimeinterval:1.0 target:self selector: @selector (Task) Userinfo:nil repeats:yes];// To create a timing source specify a method for timing source execution
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
Adds the timer source to the message loop under the current thread, specifying that the message loop mode is Nsrunloopcommanmodes
}
-(void) task{
NSLog (@ "%@", [[Nsrunloop Currentrunloop] currentmode]); Output mode of the current message loop
NSLog (@ "I am the execution method specified by the timing source of the input event");
}
Note In the above code, [[Nsrunloop Currentrunloop] currentmode] refers to the pattern of the current message loop, and the message loop pattern is a collection of input sources and timing sources, and this collection is monitored.
Each time you start a run cycle, you can specify a special mode in which only the event sources associated with a particular pattern will be monitored and the events that are allowed to pass them, and the event sources associated with other patterns will not be monitored during the run loop execution.
The above code executes the result
I added a textview to the storyboard, and the message loop pattern specified in the preceding code is nsrunloopcommanmodes. When you drag TextView, the message loop mode is Uitrackingrunloopmode, the event changes, and the corresponding message loop pattern is changed after the release is Kcfrunloopdefaultmode.
The two objects of the iOS message loop are Nsrunloop and Cfrunloop, which are encapsulated in the latter.
Cfrunloop is the object in the core Foundation framework and provides a pure C API, all of which are thread-safe.
Nsrunloop is an object in the cocoa framework and is an encapsulation of cfrunloop, and the API is not thread-safe.
Here are several system-defined message-loop patterns
| Mode |
Name |
Description |
| Default |
Nsdefaultrunloopmode (Cocoa)
Kcfrunloopdefaultmode (Core Foundation) |
Can be used for most operations. In most of the time, this mode should be used to start and set the input source.
|
| Connection |
Nsconnectionreplymode (Cocoa) |
Cocoa uses this mode to federate Nsconnection objects to listen for responses. We seldom use this pattern ourselves. |
| Modal |
Nsmodalpanelrunloopmode (Cocoa) |
Cocoa uses this mode to identify the events prepared for the modal panel. |
| Event Tracking |
Nseventtrackingrunloopmode (Cocoa) |
Cocoa uses this mode to constrain mouse dragging or other user interface tracking events. |
| Common modes |
Nsrunloopcommonmodes (Cocoa)
Kcfrunloopcommonmodes (Core Foundation) |
This is a common pattern group that uses this pattern to correlate input sources and also associates each of the patterns in the pattern group. For cocoa applications, this collection contains the default, modal, and event tracking modes. |
The Core Foundation initially contains only the default schema, but you can add custom patterns through the Cfrunloopaddcommonmode function.
| 模式 |
目的 |
普通模式的一部分? |
| kCFRunLoopDefaultMode |
默认的运行循环模式,几乎涵盖所有的来源。 你应该总是添加源和计时器这种模式下,如果没有特别的原因。 可以用符号kCFRunLoopDefaultMode和NSDefaultRunLoopMode进行访问。 |
是 |
| NSTaskDeathCheckMode |
通过使用NSTask来检查任务仍在运行。 |
是 |
_kcfhostblockingmode _KCFNETSERVICEMONITORBLOCKINGMODE  _ kcfnetservicebrowserblockingmode _KCFNETSERVICEBLOCKINGMODE  _KCFSTREAMSOCKETREADPRIVATEMODE  _ kcfstreamsocketcanreadprivatemode _kcfstreamsocketwriteprivatemode _KCFSTREAMSOCKETCANWRITEPRIVATEMODE  _ kcfstreamsocketsecuritycloseprivatemode _kcfstreamsocketbogusprivatemode _KCFURLCONNECTIONPRIVATERUNLOOPMODE  _ kproxysupportloadingpacprivatemode _ Kproxysupportsyncpacexecutionrunloopmode _kcfstreamsocketsecuritycloseprivatemode |
使用各种私人运行的循环模式CFNetwork的阻止操作 |
没有 |
| UITrackingRunLoopMode |
UI跟踪。 |
是 |
| GSEventReceiveRunLoopMode |
接收系统事件。 |
没有 |
| com.apple.securityd.runloop |
与securityd通信。 通过使用跳板而已。 |
没有 |
| FigPlayerBlockingRunLoopMode |
QuickTime的关系。 |
没有 |
In the code mentioned above, the looping pattern is changed from Kcfrunloopdefaultmode to Uitrackingrunloopmode, while the input source event of the program changes from the default to "constrain the mouse drag or other user interface to track the events of the loop." So is this example a description of the default event (most action events) that corresponds to the kcfrunloopdefaultmode of the message loop pattern, which constrains the mouse drag or other user interface to track events that correspond to the loop mode of Uitrackingrunloopmode?
Is there anyone who can tell me that since all are cyclic patterns, execute NSLog (@ "%@", [[Nsrunloop Currentrunloop] currentmode]); Why does the console output the Kcfrunloopdefaultmode and Uitrackingrunloopmode of the second table, instead of the Nsdefaultrunloopmode and nsrunloopcommonmodes of the first table?
I am a rookie ah ah, if you know please comment to me science
iOS message loop-pattern matching