创建一个定时器,时间结束后系统会使用runtime通过方法名称(Selector本质就是方法名称)去方法列表中找到对应的方法实现并调用方法。
注意事项
1.调用performSelector:withObject:afterDelay:方法时,先判断希望调用的方法是否存在respondsToSelector:
2.这个方法是异步方法,必须在主线程调用,在子线程调用永远不会调用到想调用的方法。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",[NSThread currentThread]);
[self performSelector:@selector(testThre) withObject:nil afterDelay:0];
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"%@",[NSThread currentThread]);
[self performSelector:@selector(testThre) withObject:nil afterDelay:0];
});
- (void)testThre {
NSLog(@"2222 %@",[NSThread currentThread]);
}
网友评论