美文网首页
定时执行任务的两种方法:

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

作者: 放开那个小卤蛋 | 来源:发表于2016-02-23 17:43 被阅读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];

    }

    相关文章

      网友评论

          本文标题:定时执行任务的两种方法:

          本文链接:https://www.haomeiwen.com/subject/pjobkttx.html