【iOS】performSelector详解(下篇)

2019-06-03  本文已影响0人  iwakevin

转自公众号:NA分享

performSelector延迟调用

[self performSelector:@selector(delayMethod:)
           withObject:params
           afterDelay:1];

这个方法其实是增加了一个定时器,而这时aSelector应该是被添加到了队列的最后面,所以要等当前调用此方法的函数执行完毕后,selector方法才会执行。如下:

- (void)mainMethod
{   
    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1];

    NSLog(@"调用方法==开始");

    sleep(5);

    NSLog(@"调用方法==结束");
}

- (void)delayMethod
{
    NSLog(@"执行延迟方法");
}

执行结果(注意log打印的顺序):

  调用方法==开始
  调用方法==结束
  执行延迟方法

在子线程中调用performSelector: withObject: afterDelay:默认无效,如下代码并不会打印sureTestMethodCall

dispatch_async(dispatch_get_global_queue(0, 0), ^{
    [self performSelector:@selector(sureTestMethod:)
               withObject:params
               afterDelay:3];
});
- (void)sureTestMethod:(id)objcet {
    NSLog(@"sureTestMethodCall");
}

这是因为performSelector: withObject: afterDelay:是在当前Runloop中延时执行的,而子线程的Runloop默认不开启,因此无法响应方法。 所以我们尝试在GCD Block中添加 [[NSRunLoop currentRunLoop] run];

dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self performSelector:@selector(sureTestMethod:)
                   withObject:params
                   afterDelay:3];
        [[NSRunLoop currentRunLoop]run];
    });

运行代码发现可以正常打印sureTestMethodCall。 这里有个坑需要注意,曾经尝试将[[NSRunLoop currentRunLoop] run]添加在performSelector: withObject: afterDelay:方法前,但发现延迟方法仍然不调用,这是因为若想开启某线程的Runloop,必须具有timer、source、observer任一事件才能触发开启。 简言之如下代码在执行[[NSRunLoop currentRunLoop] run]前没有任何事件添加到当前Runloop,因此该线程的Runloop是不会开启的,从而延迟事件不执行。

dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[NSRunLoop currentRunLoop]run];
        [self performSelector:@selector(sureTestMethod:)
                   withObject:params
                   afterDelay:3];
    });

延时方法,可以使用dispatch_after在子线程上执行:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
        if ([self respondsToSelector:@selector(sureTestMethod:)]) {
            [self performSelector:@selector(sureTestMethod:) withObject:params];
        }
});

performSelector取消延迟
我们在View上放置一个Button,预期需求是防止暴力点击,只响应最后一次点击时的事件。 此需求我们可以通过cancelPreviousPerformRequestsWithTarget来进行实现。cancelPreviousPerformRequestsWithTarget的作用为取消当前延时任务。在执行延迟事件前取消当前存在的延迟任务即可实现如上效果。

- (IBAction)buttonClick:(id)sender {
    id params;
    [[self class] cancelPreviousPerformRequestsWithTarget:self
                                                selector:@selector(sureTestMethod:)
                                                  object:params];
    [self performSelector:@selector(sureTestMethod:)
               withObject:params
               afterDelay:3];
}

- (void)sureTestMethod:(id)objcet {
    NSLog(@"sureTestMethodCall");
}

performSelector多线程
通过performSelectorInBackground将某selector任务放在子线程中

[self performSelectorInBackground:@selector(sureTestMethod:)
                           withObject:params];
- (void)sureTestMethod:(id)objcet {
    NSLog(@"%@",[NSThread currentThread]);
}

回到主线程执行我们可以通过方法

[self performSelectorOnMainThread:@selector(sureTestMethod)
                       withObject:params
                    waitUntilDone:NO];

waitUntilDone表示是否等待当前selector任务完成后再执行后续任务。示例如下:

- (void)main {
    NSLog(@"1");
    [self performSelectorOnMainThread:@selector(test) withObject:nil waitUntilDone:NO];
    NSLog(@"3");
}
- (void)test {
    sleep(3);
    NSLog(@"2");
}

waitUntilDoneYES时,打印1,2,3。为NO时打印1,3,2。

另外performSelector还提供了将任务执行在某个指定线程的操作

[self performSelector:@selector(sureTestMethod:)
                 onThread:thread
               withObject:params
            waitUntilDone:NO];

使用该方法一定要注意所在线程生命周期是否正常,若thread已销毁不存在,而performSelector强行执行任务在该线程,会导致崩溃:

NSThread *thread = [[NSThread alloc]initWithBlock:^{
    NSLog(@"do thread event");
}];
[thread start];
[self performSelector:@selector(sureTestMethod:)
             onThread:thread
           withObject:params
        waitUntilDone:YES];

上述代码会导致崩溃,崩溃信息为:

*** Terminating app due to uncaught exception 'NSDestinationInvalidException',
reason: '*** -[ViewController performSelector:onThread:withObject:waitUntilDone:modes:]:
target thread exited while waiting for the perform'

因为thread开启执行do thread event完毕后即退出销毁,所以在等待执行任务时Thread已不存在导致崩溃。
转自公众号:NA分享

上一篇下一篇

猜你喜欢

热点阅读