首页 > 学院 > 开发设计 > 正文

RunLoop(基本操作)

2019-11-14 20:38:25
字体:
来源:转载
供稿:网友

 

基本概念

 

-(void)runTimerInThread{    //NSAutoreleasePool,没的用    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(doSth:) userInfo:self repeats:YES];        //1.以上方法自动把NSTimer添加到RunLoop里面        NSTimer* timer=[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(doSth:) userInfo:self repeats:YES];        //2.以上方法不会自动NSTimer添加到RunLoop里面    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];    //要手动加入runLoop,额额,类方法就写入了 addTimer:timer forMode:NSDefaultRunLoopMode;            [[NSRunLoop currentRunLoop]run];    //这很重要,把定时器的线程加入运行回环runloop才会使线程处于活跃状态}

定时器要加入runLoop才可以正常运行,但是要保证定时器的精确度,必须在不堵塞的多线程下进行

 

 [self performSelectorInBackground:@selector(runTimerInThread) withObject:nil];

当然,你可以用CGD技术来处理多线程任务的

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表