NSTImer repeat the task and nstimer repeat the task

Source: Internet
Author: User

NSTImer repeat the task and nstimer repeat the task

Problem

The application needs to schedule the code for execution at a specific time. In addition, you want to repeat the task.

Solution

Run the NSTimer scheduling code at a specific time. To use nst, You need to reference the date object and the running cycle pointing to the application.

Note:

NSTimer requires a running loop. If you want to use a timer in a Mac or iOS application, you must have a running loop. This Guide requires an application with a running cycle. Sections 1.11 and 1.12 respectively describe how to create Mac and iOS applications.

Description

The code for this tutorial is in application delegation. Generally, the timer is placed in a custom class or application controller.

The timer sends messages to the object starting from the specified date and time. If the application needs to be repeated, the timer may send messages after a period of time. You must first have a date object to indicate the date and time when the timer starts to send messages to the object:
NSDate * scheduledTime = [NSDate dateWithTimeIntervalSinceNow: 10.0];

The preceding scheduling time is 10 seconds after the preceding code is executed. Any date can be used here.

Next, create a timer using the initWithFireDate: interval: target: selector: userInfo: repeats: constructor:
SString * customUserObject = @ "To demo userInfo ";

NSTimer * timer = [[NSTimer alloc] initWithFireDate: scheduledTime
Interval: 2
Target: self
Selector: @ selector (task)
UserInfo: customUserObject
Repeats: YES];

Here, we need to describe some content. The first parameter is a date object that specifies when the timer changes to the active state. Next is the interval, which is the number of seconds that the timer needs to wait before sending the message again. Next is the target parameter descriptor, and the target is the object of the method. The selector parameter is the method name in parentheses, followed by the @ selector keyword. The self keyword can be used because the method and the timer are both in the application delegate.

UserInfo is the custom content used by the timer. You can use any object and get the object reference in the message being executed (the selector parameter above ). Strings are used here, But dictionaries or other sets are usually used to support more complex activities.

The repeats parameter indicates whether the timer sends a message once or according to the time interval specified by the 2nd parameters.

Next we need to point to the reference of the running loop. The reference can be obtained through the currentRunLoop function of the nsunloop:
Nsunloop * runLoop = [nsunloop currentRunLoop];

Now, you only need to add the timer to the running loop:
[RunLoop addTimer: timer forMode: NSDefaultRunLoopMode];

After 10 seconds, the timer starts to send task messages to the application every two seconds.

After the timer is started, if you want to stop the timer, you can send the invalidate message to the timer. This will delete the timer from the running loop. The Code is as follows:
[Timer invalidate];

See program list 5-7.

Code

Program list 5-7 main. m
# Import "AppDelegate. h"

@ Implementation AppDelegate
@ Synthesize window = _ window;

-(Void) applicationDidFinishLaunching :( NSNotification *) aNotification {
NSDate * scheduledTime = [NSDate dateWithTimeIntervalSinceNow: 10.0];
NSString * customUserObject = @ "To demo userInfo ";
NSTimer * timer = [[NSTimer alloc] initWithFireDate: scheduledTime
Interval: 2
Target: self
Selector: @ selector (task)
UserInfo: customUserObject
Repeats: YES];
Nsunloop * runLoop = [nsunloop currentRunLoop];
[RunLoop addTimer: timer forMode: NSDefaultRunLoopMode];
}

-(Void) task :( id) sender {
NSTimer * localTimer = (nstmer *) sender;
NSLog (@ "Schedule task has executed with this user info: % @", [localTimer userInfo]);
}

@ End

Use

To use the above Code, build and run the Mac application from Xcode. Note that the console window and when the message will be printed to the log. The timestamp is printed here, so that you can see the operation method of the time interval:
15:23:28. 651 Timer [31067: 707] Schedule task has executed with this user info: To demo userInfo
15:23:30. 651 Timer [31067: 707] Schedule task has executed with this user info: To demo userInfo
15:23:32. 651 Timer [31067: 707] Schedule task has executed with this user info: To demo userInfo


How can I set the number of NSTimer repetitions?

+ Usage: invocation: repeats: + usage: target: selector: userInfo: repeats: + usage: invocation: repeats: + timerWithTimeInterval: target: selector: userInfo: repeats:-initWithFireDate: interval: target: selector: userInfo: repeats:
 
Let's see what's wrong with my NSTimer.

Wait for the great gods to solve the problem. Generally, it won't happen. Can it be done in debug mode ··
 

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.