performSelector最全讲解

1、明确方法所在位置

分别在NSObject.h 、NSRunloop.h 、NSThread.h 三个类中,是不是很神奇。(其中在NSRunloop.h 、NSThread.h文件中都是NSObject的分类)


位置1.jpeg
位置2.jpeg
位置3.jpeg

2、在NSObject.h中的方法

1、 - (id)performSelector:(SEL)aSelector;
2、 - (id)performSelector:(SEL)aSelector withObject:(id)object;
3、 - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
三个方法均为同步执行,与线程无关,在主线程和子线程中均可调用。等同于直接调用该方法。
与直接调用方法区别:直接调用编译时会自动校验。performSelector在运行时去找方法,在编译时不做校验。

3、在NSRunloop.h中的方法

1、- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes;
2、- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;

3.1、这两个方法为异步执行,delay值为0,仍为异步执行。

1、在主线程执行,方法调用成功。
2、在子线程执行,需要开启子线程Runloop,方法才可以调用成功。
注意:调用该方法之前或在该方法所在的VC释放时主动调用取消函数,以确保不会内存泄露。

3.2、可控制延迟调用方法的取消操作:

1、+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
2、+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;

4、在NSThread.h中的方法

4.1、在主线程调用方法

1、- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
2、- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
在主线程和子线程中均可执行,都会在主线程调用Selector方法。

4.1.1、在子线程中设置wait值

wait=YES:当前线程被阻塞,主线程执行完Selector,接着执行。

- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_queue_create("cwwng-queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
       
        NSLog(@"1");
        [self performSelectorOnMainThread:@selector(testAction) withObject:nil waitUntilDone:YES];
        NSLog(@"2");
    });
    
}

- (void)testAction {
    
    sleep(3);
    NSLog(@"testAction");
}
2020-11-21 20:26:12.252984+0800 CwwngDemo[28225:462843] 1
2020-11-21 20:26:15.289730+0800 CwwngDemo[28225:462744] testAction
2020-11-21 20:26:15.289991+0800 CwwngDemo[28225:462843] 2

wait=NO:当前线程不被阻塞。

- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_queue_t queue = dispatch_queue_create("cwwng-queue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
       
        NSLog(@"1");
        [self performSelectorOnMainThread:@selector(testAction) withObject:nil waitUntilDone:NO];
        NSLog(@"2");
    });
    
}

- (void)testAction {
    
    sleep(3);
    NSLog(@"testAction");
}
2020-11-21 20:28:50.383884+0800 CwwngDemo[28347:465953] 1
2020-11-21 20:28:50.384127+0800 CwwngDemo[28347:465953] 2
2020-11-21 20:28:53.410808+0800 CwwngDemo[28347:465859] testAction
4.1.2、在主线程中设置wait值

wait=YES:等待Selector执行完,再接着执行。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"1");
    [self performSelectorOnMainThread:@selector(testAction) withObject:nil waitUntilDone:YES];
    NSLog(@"2");
}

- (void)testAction {
    
    sleep(3);
    NSLog(@"testAction");
}
2020-11-21 20:33:53.966768+0800 CwwngDemo[28608:472716] 1
2020-11-21 20:33:56.967413+0800 CwwngDemo[28608:472716] testAction
2020-11-21 20:33:56.967738+0800 CwwngDemo[28608:472716] 2

wait=NO:不等待Selector执行完,接着执行。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"1");
    [self performSelectorOnMainThread:@selector(testAction) withObject:nil waitUntilDone:NO];
    NSLog(@"2");
}

- (void)testAction {
    
    sleep(3);
    NSLog(@"testAction");
}
2020-11-21 20:38:55.042160+0800 CwwngDemo[28838:477847] 1
2020-11-21 20:38:55.042508+0800 CwwngDemo[28838:477847] 2
2020-11-21 20:38:58.087345+0800 CwwngDemo[28838:477847] testAction
4.2、在指定线程调用方法
  • (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
  • (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
4.3、开启子线程在后台运行
  • (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

推荐阅读更多精彩内容