Nsrunloop (from official documents)

Source: Internet
Author: User

The NSRunLoop class declares the programmatic interface to objects that manage input sources. An NSRunLoop object processes input to sources such as mouse and keyboard events from the window System, NSPort objects, and c3/> objects. An NSRunLoop object also processes NSTimer events.

Your application cannot either create or explicitly manage Nsrunloop objects. Each NSThread object, including the application ' s main thread, has a NSRunLoop object automatically created for it as needed. If you are need to access the current thread's run loop, you have the with the class method currentRunLoop .

Note that from the perspective NSRunloop of, NSTimer objects is not "input"-they is a special type, and one of the things that Means is that they does not cause the run loop to return when they fire.

Attention:

Nsrunloop is generally not considered thread-safe and should only be called in the context of the current thread. Do not attempt to invoke a method of the Nsrunloop object in another different thread, which can cause an unknown error.

The timer and nsrunloop work together, but they can't provide a real time mechanism for accuracy limitations. If you just want to send a message at some point in the future, you don't have to timer,timer the Nstimer object.

Timers is represented by NSTimer objects. They work NSRunLoop in conjunction with objects. NSRunLoop Objects Control loops This wait for input, and they use timers to help D Etermine the maximum amount of time they should wait. When the timer's time limit has elapsed, the run loop fires the timer (causing it message to being sent), then checks for NE W input.

The run loop mode in which you register the timer must is running for the timer to fire. For applications built using the application Kit or UIKit, the Application object runs the main thread ' s run loop for you. On secondary threads, however, you had to run the run loop yourself.

Runloop is a thread-related infrastructure. A runloop is a time-processing loop where you can schedule work and coordinate the receipt of incoming events. The purpose of Runloop is to have a job, to keep the thread busy, not to work, and let the thread sleep.

Run loop management is not fully automated. You must design your thread code, start the run loop at the appropriate time, and then the event that arrives accordingly. Cocoa and core Foundation provide Runloop objects to help you configure Runloop to manage your threads. It is not necessary for your application to explicitly create these objects; Each thread (including the application main thread) has an associated Runloop object. Only the Secondar thread needs to explicitly run their runloop. The app framework (frameworks) automatically starts and runs Runloop on the main thread as part of the program startup process.

Anatomy of a Run Loop

A run loop is very much like its name sounds. It is a loop your the thread enters and uses to run event handlers in response to incoming events. Your Code provides the control statements used to implement the actual loops portion of the run loop-in other words, Your c ODE provides the while or for loop that drives the run loop. Within your loop, you use a run loop object to "Run" the Event-processing code that receives events and calls the Installe D handlers.

The run loop receives events from two different types of sources. Input sources deliver an asynchronous event, typically a message from another thread or a different application. Timer source deliver, not much explained. When these two provenances arrive, they use the application-specific processing path.

Figure 3-1 below shows the conceptual structure of the run loop and various sources (source). The Input source deliver an asynchronous event to the appropriate processor and causes the rununtildate: method (called by the Runloop object associated with the thread) to exit. Timer source deliver events to their handler routines (understanding, translation does not pass, there is an English-speaking sister how good), but will not cause runloop exit.

In addition to handling sources of input, run loops also generate notifications about the run loop ' s behavior. Registered Run-loop observers can receive these notifications and use them to does additional processing on the thread. You use the Core Foundation to install the Run-loop observers on your threads.

The following content provides more information about Runloop components and patterns, and also describes the notifications generated at different times during the processing of events.

Run Loop Modes

a run loop mode is a collection of input sources and timers to being monitored and a collection of run loop observers to Be notified. Each time you run the loop, you specify (either explicitly or implicitly specified) a particular "mode" in the which to run. During that pass of the run loop, only sources associated with that mode is monitored and allowed to deliver their events . (Similarly, only observers associated with that mode is notified of the run loop ' s progress.) Sources associated with and modes hold on to all new events until subsequent passes through the loop in the appropriate Mode. (The key thing dare not to translate ah, timid)

In your code, the different patterns (modes) are distinguished by their names. The Cocoa and core Foundation define a default and several common patterns. You can customize the pattern by specifying a string for the name of the pattern. The name of the pattern can be random, but the content can not. You want to make sure that one or more of the input sources, timers, or run-loop observers are added to the pattern you created.

You use the modes to filter out events from unwanted sources during a particular pass through your run loop. Most of time, you'll want to run your run loop in the system-defined "default" mode. A modal panel, however, might run in the "modal" mode. While in this mode, only sources revevant to the model panel would deliver events to the thread. For secondary threads, you might use custom modes to prevent low-priority sources from delivering events during Time-criti Cal operations.

Note:Modes discriminate based on the source of the event, not the type of the event. For example, you would not use modes to match only Mouse-down events or only keyboard events. You could use modes to listen to a different set of ports, suspend timers temporarily, or otherwise change the sources and Run loop observers currently being monitored.

Digest and Digest, eat to go. Sometimes it goes on.

Nsrunloop (from official documents)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.