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

2016-02-23  本文已影响29人  放开那个小卤蛋

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

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

}

上一篇下一篇

猜你喜欢

热点阅读