- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
——此方法是runloop方法姆泻,只有開啟runloop才能執(zhí)行氛雪。主線程默認(rèn)開啟runloop谋梭,子線程要手動(dòng)開啟锈拨、關(guān)閉十拣。
注:開啟runloop方法要寫在performSelector方法之后才有效果叙身。
一死讹、子線程開啟runloop:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
distantFuture (不可達(dá)到的未來的某個(gè)時(shí)間點(diǎn))
distantPast (不可達(dá)到的過去的某個(gè)時(shí)間點(diǎn))
二、子線程關(guān)閉runloop:
CFRunLoopStop([NSRunLoop currentRunLoop].getCFRunLoop);
三曲梗、子線程開啟赞警、關(guān)閉runloop實(shí)例:
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已關(guān)閉,不會(huì)執(zhí)行虏两。
}