定時(shí)執(zhí)行任務(wù)的兩種方法:
一沼沈、- (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];
}
想要停止執(zhí)行:[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];