·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> RunLoop(基本操作)

RunLoop(基本操作)

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

 

基本概念

 

-(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技术来处理多线程任务的