Basic use of Nstimer in iOS development

Source: Internet
Author: User

1. Background: Nstimer What the hell is this?

The goods everybody from its "face" estimate can know what it is, yes, the timer. However, if you say Nstimer is a timer, someone will say that you are too amateur, the term is inaccurate, and then in a bull's gesture to say that some normal people do not understand and have a sarcastic taste. Oh, this is not important, because everyone is being abused by Daniel, in the future will be to abuse others. The important thing is what nstimer can do, this is what we care about, as to its definition or concept, or to the English Department of children's shoes to worry about it. So, before we introduce the function of Nstimer, let's just call it a timer, and it can do that from now to a certain point in the back, or periodically to the object that executes the method we specify.

2. Knowledge to know before using Nstimer

(1) Nstimer has the following two common methods

+ (Nstimer *) Timerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector userInfo: (ID) UserInfo repeats: (BOOL) Yesorno;

Create a timer, parameter 1: time interval (seconds), Parameter 2: Indicates the object being sent (general fill self), Parameter 3: The method to be executed, parameter 4: Passing information (which can be passed to the method to be executed in the form of a dictionary), parameter 5: Whether repeated execution, if no, The timer executes once and then fails.

+ (Nstimer *) Scheduledtimerwithtimeinterval: (nstimeinterval) TI target: (ID) atarget selector: (SEL) Aselector UserInfo :(ID) UserInfo repeats: (BOOL) Yesorno;

Create a timer, parameter 1: time interval (seconds), Parameter 2: Indicates the object being sent (general fill self), Parameter 3: The method to be executed, parameter 4: Passing information (which can be passed to the method to be executed in the form of a dictionary), parameter 5: Whether repeated execution, if no, The timer executes once and then fails.

You may find that the first method and the second method seem to be the same. Yes, they are basically the same, but there are some differences, continue to look down, you will know their differences.

(2) Running cycle Runloop

I do not know if you have ever thought about this problem, an application will be placed there after the start of operation, if it does not do anything, the application is like a static, no spontaneous action occurs, but if we click a button on the interface, this time there will be a corresponding button response event occurs. It gives us the feeling that the application is always on standby, that it is resting when no one is working, and that it responds immediately when it is allowed to work. In fact, this is the credit of the run loop. (This passage of the stockade csdn)

You can think of the running cycle as a system-level thread, which is constantly listening to the various events in the system, and once an event occurs, he triggers the program in the running loop. The same is true for Nstimer, which runs only when you join a running loop. However, joining the running cycle is also divided into a variety of situations, like we go to work, Gaofu Drive, ordinary youth to do the subway, literary youth do not go. Nstimer Join the Run loop there are two common modes, the first of which is the default mode (Nsdefaultrunloopmodel), and the second is (Nsrunloopcommonmodels).

(3) The difference between Timerwithtimeinterval and Scheduledtimerwithtimeinterval

Knowing the two modes of running the loop, we can tell what the difference is between the two methods. The Scheduledtimerwithtimeinterval method automatically joins the run loop in Nsdefaultrunloopmodel mode after the timer is created. The timer created by the Timerwithtimeinterval method does not automatically join the run loop, requiring us to manually specify the pattern and manually join the run loop.

As for the difference between the two modes, we'll get the answer through the code.

3. Some examples

Example 1:

The above example shows that the Scheduledtimerwithtimeinterval method creates a timer and then joins the run loop for execution, and the UserInfo information can be carried into the method via parameters.

Example 2:

Also the above code, we added the TextView control in the interface, in the process of the timer execution, we drag the TextView control, found that the timer will break, stop dragging TextView, the timer continues to execute.

The code above shows that in the default mode (Nsdefaultrunloopmodel), when a scrolling event occurs, the run loop will temporarily stop the execution of the Nstimer, and then continue executing the Nstimer timer after the scroll event has ended. So what happens in another model (nsrunloopcommonmodels)? Let's take a look again, of course, to use the Nsrunloopcommonmodels mode, we'll use the Timerwithtimeinterval method to create the timer.

Example 3:

In the above code we found that, in nsrunloopcommonmodels mode, no matter how we scroll the view, Nstimer will also execute normally.

The above example illustrates the phenomenon of the two modes, in fact, the difference between the two modes is:

Nsdefaultrunloopmodel: Listen to the user's most basic operation (click, Touch, etc.)

Nsrunloopcommonmodels: Monitoring Some Special operations: scrolling, etc.

So why does the timer stop when scrolling occurs in Nsdefaultrunloopmodel mode? That is because the system thinks that the user should not scroll the side of the interface, so stop (touch, click, etc.) Nsdefaultrunloopmodel mode listening events.

4. Using Nstimer is not accurate timing

Assuming that Nstimer is going to perform a,b,c,d four tasks and is interrupted when executed to B, the b,c will be executed together when Nstimer is given the right to continue execution. In other words, Nstimer does not omit the task, but it cannot guarantee that the task will be executed on time. So, how does iOS ensure that tasks are performed on time? In general, we will use Cadisplaylink. It will be executed once each time the screen is refreshed. The screen's refresh rate is up to dozens of times per second, guaranteeing the accuracy of the time.

The above content is the individual to nstimer superficial understanding, in the future if have further understanding, also will update the article. You are welcome to criticize and correct me.

Finish

Basic use of Nstimer in iOS development

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.