RunLoop与PerformSelector

PerformSelecter

当调用 NSObject 的 performSelecter:afterDelay: 后,实际上其内部会创建一个 Timer 并添加到当前线程的 RunLoop 中。所以如果当前线程没有 RunLoop,则这个方法会失效。

当调用 performSelector:onThread: 时,实际上其会创建一个 Timer 加到对应的线程去,同样的,如果对应线程没有 RunLoop 该方法也会失效。

其他的performSelector系列方法是类似的

直接上代码

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self tryPerformSelectorOnMianThread];
    
    __weak typeof(self) weakSelf = self;
    //backGroundThread并不会被调用到,这是因为,在调用performSelector:onThread: withObject: waitUntilDone的时候,系统会给我们创建一个Timer的source,加到对应的RunLoop上去,然而这个时候我们没有RunLoop
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [weakSelf tryPerformSelectorOnbackGroundThread1];
    });
    
    //在tryPerformSelectorOnbackGroundThread1上我们添加个RunLoop,此时backGroundThread2就可以被调用到
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [weakSelf tryPerformSelectorOnbackGroundThread2];
    });
    
    //那么为什么主线程中的perfom selector却能够正常调用呢?通过上面的例子相信你已经猜到了,主线程的RunLoop是一直存在的,所以我们在主线程中执行的时候,无需再添加RunLoop。
    //小结:当perform selector在后台线程中执行的时候,这个线程必须有一个开启的runLoop
}

- (void)tryPerformSelectorOnMianThread{
    [self performSelector:@selector(mainThreadMethod) withObject:nil];
}

- (void)tryPerformSelectorOnbackGroundThread1{
    //backGroundThread并不会被调用到,这是因为,在调用performSelector:onThread: withObject: waitUntilDone的时候,系统会给我们创建一个Timer的source,加到对应的RunLoop上去,然而这个时候我们没有RunLoop
    [self performSelector:@selector(backGroundThread1) onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO];
    //默认是会在MainRunLoop中去执行,故他可以调用到backGroundThread
//    [self performSelector:@selector(backGroundThread1) withObject:nil];
    [self tryPerformSelectorOnbackGroundThread2];
}

- (void)tryPerformSelectorOnbackGroundThread2{
    //在tryPerformSelectorOnbackGroundThread1上我们添加个RunLoop,此时backGroundThread2就可以被调用到
    [self performSelector:@selector(backGroundThread2) onThread:[NSThread currentThread] withObject:nil waitUntilDone:NO];
    [[NSRunLoop currentRunLoop] run];
}

- (void)mainThreadMethod{
    NSLog(@"execute %s",__func__);
}

- (void)backGroundThread1{
    NSLog(@"currentThread:%@",[NSThread currentThread]);
    NSLog(@"backGroundThread1 --> execute %s",__FUNCTION__);
}

- (void)backGroundThread2{
    NSLog(@"currentThread:%@",[NSThread currentThread]);
    NSLog(@"backGroundThread2 --> execute %s",__FUNCTION__);
}

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值