RunLoop与PerformSelector

RunLoop与PerformSelector

PerformSelecter

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

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

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

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@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__);
}
------本文结束😘感谢阅读------