- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
——此方法是runloop方法,只有开启runloop才能执行。主线程默认开启runloop,子线程要手动开启、关闭。
注:开启runloop方法要写在performSelector方法之后才有效果。
一、子线程开启runloop:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
distantFuture (不可达到的未来的某个时间点)
distantPast (不可达到的过去的某个时间点)
二、子线程关闭runloop:
CFRunLoopStop([NSRunLoop currentRunLoop].getCFRunLoop);
三、子线程开启、关闭runloop实例:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self performSelector:@selector(asdf) withObject:nil afterDelay:1];
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
});
-(void)asdf
{
CFRunLoopStop([NSRunLoop currentRunLoop].getCFRunLoop);
dispatch_sync(dispatch_get_global_queue(0, 0), ^{
[self performSelector:@selector(asdasd) withObject:nil afterDelay:5];
});
}
-(void)asdasd
{
//runloop已关闭,不会执行。
}
网友评论