IOS Runloop Learning Notes

Source: Internet
Author: User

One,* What's run loop *

1, A run loop is an abstraction that (among other things) provides A mechanism to handle system input sources (sockets, Por TS, files, keyboard, mouse, timers, etc).

Each nsthread have its own run loop, which can be accessed via the Currentrunloop method.

In general, you don't need to access the run loop directly, though there is some (networking) components You to specify which run loop they would use the for I/O processing.

A run loop for a given thread would wait until one or more of its input sources have some data or event, then fire the Appro Priate input handler (s) to process each of the input source is ' ready. '

After doing so, it'll then return to its loop, processing input from various sources, and "sleeping" if there are no work To do.

2, first consider this problem: your cocoa program Most of the time did not do anything, more specific point, is waiting for input. However, once you touch the screen and the corresponding event is triggered, you may be able to execute your piece of event-handling code. Similarly, the return of some data in the socket, or the timer trigger is the same situation. And more importantly, once the code that triggered the event finishes executing, the program goes back to the wait state. In many cases, the code executes much less time than the program waits for input.

I think run loop is a good way to take advantage of this fact. A run loop is a loop that runs on a single thread for event processing. You register the input source on the run loop and specify the code that should be executed when these sources have input. When there is input on a particular source, the run loop executes the corresponding code and then continues to wait for the next input event. If the input to another source is in progress while the run loop is executing the processing code, the run loop processes the input event after the current processing is completed. The advantage is that although you don't know the exact input order, you know that they will eventually be serially processed one after the other. This means that you will not encounter multi-threading issues, which is why the run loop is very useful.

Runloop this thing, in fact we have been using, but has not been well understood it, or even did not know its existence. Runloop can be said to be an object for each thread, which is the loop used to accept events and assign tasks. Never create a runloop manually, it is followed by each thread. An runloop receives two types of source events: input source and timer source. It is also important to know that input source,runloop is delivered asynchronously, while the timer source is delivered synchronously. Each runloop has a runloop Modes that represents how it is executed.

Why do we never feel the existence of runloop, because when the program starts, the system defaults to help us start a runloop of the main thread, and runs until the program exits. The user-created sub-thread, Runloop, needs to be started manually, so the thread starts the timer or calls PerformSelector:withObject:afterDelay:inModes: it needs to start runloop.

Runloop literally is the meaning of running a loop, which is also good, it is really a loop concept, or exactly a thread in the loop. At the beginning of this article, it was mentioned that some programs are a circle, and this circle is essentially the so-called Runloop, which is a loop, but it is a loop that adds many features.
First the loop body needs to detect if there are events that need to be handled, and if so, go to sleep to save CPU time. So the focus is this need to deal with the event, in the Runloop, there are two types of events that need to be handled, one is the input source, the other is the timer, the timer is a good understanding of the operation that needs to be timed, the input source is divided into three categories: Performselector source, Port-based (Mach Port), and the source of the customization. You can add your own source when you are programming.

Second,* use of Run loop *

By default, all touch events on the iphone are queued for processing by the main run loop, so you don't need to do extra work on the UI component, while other input sources require some extra coding. For example, schedule a nsinputstream on the run loop, you need to look like this:

[iStream setDelegate:self];[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];

In the above code, once IStream has input data, it executes the stream:handleevent method of self. And this stream can be any type of input source, including the socket.

In addition, the timer object can be schedule on the run loop, for example:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selectornil repeats:YES];

The above code schedule the timer to the current run loop and calls the self's Dostuff method every 2 seconds.

Third,* Not applicable to run loop condition *

When is it not appropriate to use Run loop? Depending on the characteristics of the run loop, the input event is serially processed one after the other, so if the processing of an event takes a very long time, the app will not be able to respond to other input events until the event is processed. In this case, it is more appropriate to open a new threading process. However, in most cases, our code handles screen, socket, or timer events very quickly, which is easier and more secure to use with main run loop.

Reference Links:

    1. Threading Programming Guide
    2. Threading Programming Guide Translation
    3. Objective-c's Run Loop detailed
    4. Runloop in iOS
    5. Understanding Nsrunloop
    6. Run Loop and Thread
    7. iOS about Runloop and timer
    8. A preliminary study on iOS multi-Threading (iii) –nsrunloop

IOS Runloop Learning notes

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.