Ios oc multi-task timer nsunloop management NSTimer

Source: Internet
Author: User

The following two methods are available:

1. Use the date component NSDateComponents

2. Use NSString to generate a date

// Create a Calendar Object

NSCalendar * calendar = [NSCalendarcurrentCalendar];

// Create date component

NSDateComponents * dc = [[NSDateComponentsalloc] init];

// Set the current time

[Dc setCalendar: calendar];

[Dc setYear: 2014];

[Dc setMonth: 4];

[Dc setDay: 9];

[Dc setHour: 21];

[Dc setMinute: 26];

[Dc setSecond: 0];


NSString * dateStr = @ "2014-04-0921: 38: 00 ";

NSDateFormatter * formatter = [[NSDateFormatteralloc] init];

Formatter. dateFormat = @ "yyyy-MM-ddHH: mm: ss ";

NSDate * schuduleNate = [dcdate];

NSDate * schuduleNate2 = [formatterdateFromString: dateStr];

NSDate * dateNow = [NSDatedate];

// Calculate the time difference

NSTimeInterval num = [schuduleNate2timeIntervalSinceDate: dateNow];

NSLog (@ "% f and then start executing the task", num );

// Task pool

Nsunloop * mainLoop = [nsunloopcurrentrunloop];


//

NSTimer * timer = [NSTimerscheduledTimerWithTimeInterval: num target: self selector: @ selector (scheduleTask) userInfo: nil repeats: NO];

NSTimer * timer2 = [NSTimerscheduledTimerWithTimeInterval: num + 3 target: self selector: @ selector (scheduleTask) userInfo: nil repeats: NO];

// Add to task pool

[MainLoop addTimer: timer forMode: NSDefaultRunLoopMode];

[MainLoop addTimer: timer2 forMode: NSDefaultRunLoopMode];

[MainLoop run];


Output Information

21:37:54. 289 WeChat [1810: 60b] 5.710526

21:38:00. 001 WeChat [1810: 60b] do some thing

21:38:03. 001 WeChat [1810: 60b] do some thing


Related Article

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.