定时执行任务的两种方法:

定时执行任务的两种方法:

、- (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];

}

你可能感兴趣的:(定时执行任务的两种方法:)