【iOS】performSelector详解(下篇)

转自公众号: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分享

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基本概念 进程 进程是指在系统中正在运行的一个应用程序,而且每个进程之间是独立的,它们都运行在其专用且受保护的内存...
    小枫123阅读 929评论 0 1
  • 翻译来源: RunLoops Run Loops RunLoops是与线程紧密相关的基础架构的一部分,简称运行循环...
    AlexCorleone阅读 603评论 0 1
  • 文章目录RunLoop简介1.1 什么是RunLoop? 1.2 RunLoop和线程1.3 默认情况下主线程的R...
    lusen_b阅读 405评论 0 2
  • 文章目录 RunLoop简介 1.1 什么是RunLoop? 1.2 RunLoop和线程 1.3 默认情况下主线...
    咖啡绿茶1991阅读 300评论 0 0
  • 2017-08-05iOS开发 1.启动RunLoop 通过[NSRunLoop currentRunLoop]或...
    C9090阅读 1,280评论 0 0