本文转载至 http://www.mgenware.com/blog/?p=463
1. 对当前Run Loop中Selector Sources的取消
NSObject中的performSelector:withObject:afterDelay:方法将会在当前线程的Run Loop中根据afterDelay参数创建一个Timer,如果没有调用有inModes参数的方法,该Timer会运行在当前Run Loop的默认模式中,也就是NSDefaultRunLoopMode定义的模式中。
performSelector:withObject:afterDelay:方法的使用看起来还是很简单的。这里讲另外一个辅助函数,NSObject中静态的cancelPreviousPerformRequestsWithTarget方法。该方法就是专门用来取消取消performSelector:withObject:afterDelay:方法所创建的Selector source(内部上就是一个Run Loop的Timer source)。因此该方法和performSelector:withObject:afterDelay:方法一样,只限于当前Run Loop中。
我们可以利用cancelPreviousPerformRequestsWithTarget直接取消一个对象在当前Run Loop中的所有未执行的performSelector:withObject:afterDelay:方法所产生的Selector Sources,如下代码:
- (void)viewDidLoad{ [super viewDidLoad]; [self performSelector:@selector(test:) withObject:nil afterDelay:1]; [self performSelector:@selector(test:) withObject:@"mgen" afterDelay:2]; [NSObject cancelPreviousPerformRequestsWithTarget:self]; } - (void)test:(id)obj{ NSLog(@"调用成功: %@", obj);}
不会有任何输出,因为两个调用都被取消了。
如果想取消单独一个的话,需使用cancelPreviousPerformRequestsWithTarget:selector:object:方法,注意selector和object参数需要一一对应。如下代码:
- (void)viewDidLoad{ [super viewDidLoad]; [self performSelector:@selector(test:) withObject:[NSNumber numberWithInt:26] afterDelay:1]; [self performSelector:@selector(test:) withObject:[NSNumber numberWithInt:17] afterDelay:2]; [self performSelector:@selector(test:) withObject:[NSNumber numberWithInt:17] afterDelay:3]; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(test:) object:[NSNumber numberWithInt:17]]; } - (void)test:(id)obj{ NSLog(@"调用成功: %@", obj);}
只会输出:
调用成功: 26
其他两个Selector都被取消了。
2. 在NSThread中执行Selector
这个话题很简单,直接通过NSObject的performSelectorInBackground:withObject:方法就可以,如下代码:
- (void)viewDidLoad{ [super viewDidLoad]; [self threadInfo:@"UI"]; [self performSelectorInBackground:@selector(test:) withObject:nil]; } - (void)test:(id)obj{ @autoreleasepool { [self threadInfo:@"test"]; }}- (void)threadInfo:(NSString*)category{ NSLog(@"%@ - %@", category, [NSThread currentThread]);}
输出:
UI - <NSThread: 0x71639e0>{name = (null), num = 1}test - <NSThread: 0x7176ad0>{name = (null), num = 3}
这个方法完全等效于NSThread的detachNewThreadSelector:toTarget:withObject:静态方法,那么上面NSObject的performSelectorInBackground:withObject:方法调用完全可以替换成:
[NSThread detachNewThreadSelector:@selector(test:) toTarget:self withObject:nil];
当然,用户也可以自行手动创建一个NSThread来完成上述功能,代码如下:
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(test:) object:nil];[thread start];
这两种方法运行后的输出是和第一种类似的。
3. 在NSThread中的Run Loop中执行Selector
这里需要的方法是NSObject的performSelector:onThread:withObject:waitUntilDone:方法。由于是在另一个NSThread中执行Selector,所以我们需要手动开始Run Loop。首先需要在ViewController中定义两个字段,分别是NSThread和控制线程内Run Loop执行的flag。
@interface ViewController (){ NSThread *_thread; BOOL _isNewThreadAborted;}
接下来做的是执行这个线程,并且在线程中手动调用NSRunLoop的runMode:beforeDate:方法。这里注意,如果Run Loop没有任何Source的话,该方法会立即返回,所以需要创建一个循环来持续调用Run Loop的runMode:beforeDate:方法。并在Selector执行结束后同时尝试结束这个循环。最终代码如下:
- (void)viewDidLoad{ [super viewDidLoad]; [self threadInfo:@"UI"]; _isNewThreadAborted = NO; _thread = [[NSThread alloc] initWithTarget:self selector:@selector(newThread:) object:nil]; //开始线程 [_thread start]; //在另一个线程中的Run Loop中执行Selector [self performSelector:@selector(test:) onThread:_thread withObject:nil waitUntilDone:NO]; } //在新线程中创建并开始一个NSRunLoop- (void)newThread:(id)obj{ @autoreleasepool { NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop]; while (!_isNewThreadAborted) { [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } NSLog(@"线程停止"); }} //Selector执行- (void)test:(id)obj{ [self threadInfo:@"test"]; _isNewThreadAborted = YES;} - (void)threadInfo:(NSString*)category{ NSLog(@"%@ - %@", category, [NSThread currentThread]);}
输出:
UI - <NSThread: 0x717e7e0>{name = (null), num = 1}test - <NSThread: 0x8078a80>{name = (null), num = 3}线程停止
最后注意performSelector:onThread:withObject:waitUntilDone:方法中最后的waitUntilDone参数,如果传YES的话,当前线程会等待Selector在另一个线程中执行完毕后继续执行。