解决performSelector afterDelay在子线程不调用问题

有时候你会发现performSelector延时不起作用,啥原因呢
先看下面的例子

- (void)viewDidLoad {
    [super viewDidLoad];
    NSThread * thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
    [thread setName:@"com.will.thread"];
    [thread start];
}

- (void)threadRun{
    [self performSelector:@selector(runTime1) withObject:nil];
    [self performSelector:@selector(runTime2) withObject:nil afterDelay:1];
    [self performSelector:@selector(runTime2) withObject:nil afterDelay:1 inModes:@[NSDefaultRunLoopMode]];
}

- (void)runTime1{
    NSLog(@"xxoo1");
}

- (void)runTime2{
    NSLog(@"xxoo2");
}
image.png

为什么延时函数没有调用,因为你在子线程里调用延时函数,需要定时器,而子线程不同于主线程不会自动创建runloop,导致定时器没有工作,
解决方法有四种:

  • 在子线程里启动runloop
- (void)threadRun{    
    [self performSelector:@selector(runTime2) withObject:nil afterDelay:1];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
    [runLoop run];
}
  • performSelector设定主线程,不过没法设置延时函数
- (void)threadRun{
    [self performSelector:@selector(runTime2) onThread:[NSThread mainThread] withObject:nil waitUntilDone:NO];
    //或者:
    [self performSelectorOnMainThread:@selector(runTime2) withObject:nil waitUntilDone:NO];
}
  • 专门设定在已经开启了runloop的线程
- (void)threadRun{
    [self performSelector:@selector(runTime2) onThread:[[self class] _networkThread] withObject:nil waitUntilDone:NO modes:@[NSDefaultRunLoopMode]];
}

+ (void)_addRunLoop:(NSThread *)thread{
    @autoreleasepool {
        [[NSThread currentThread] setName:@"com.will.webimage.request"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}

+ (NSThread *)_networkThread{
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(_addRunLoop:) object:nil];
    [NSThread sleepForTimeInterval:1];
    [thread start];
    return thread;
}
  • 直接用GCD的延时方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1*NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self runTime2];
});

注意:

  • performSelector若不指定mode([self performSelector:@selector(method) withObject:nil];),默认自动创建mode为NSDefaultRunLoopMode

  • performSelector:onThread:withObject:waitUntilDone里waitUntilDone为NO,则会等待当前线程执行完,再执行selector里的方法

- (void)threadRun{    
    [self performSelector:@selector(runTime2) onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO];
    
    NSLog(@"com.will.thread执行完毕");
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
    [runLoop run];
}

打印如下


image.png

你可能感兴趣的:(解决performSelector afterDelay在子线程不调用问题)