定时执行任务的两种方法:
一、- (void)startLoop
{
NSDate *scheduledTime = [NSDate dateWithTimeIntervalSinceNow:10.0];
NSString *customUserObject = @"To demo userInfo";
timer = [[NSTimer alloc] initWithFireDate:scheduledTime
interval:10
target:self
selector:@selector(refresh)
userInfo:customUserObject
repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
}
想要停止执行:[timer invalidate];
二、
- (void)startLoop
{
[NSThread detachNewThreadSelector:@selector(loopMethod) toTarget:self withObject:nil];
}
- (void)loopMethod
{
[NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(refresh) userInfo:nil repeats:YES];
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[loop run];
网友评论