1塌西、明確方法所在位置
分別在NSObject.h 柔吼、NSRunloop.h 掰邢、NSThread.h 三個類中牺陶,是不是很神奇。(其中在NSRunloop.h 尸变、NSThread.h文件中都是NSObject的分類)
位置1.jpeg
位置2.jpeg
位置3.jpeg
2义图、在NSObject.h中的方法
1、 - (id)performSelector:(SEL)aSelector;
2召烂、 - (id)performSelector:(SEL)aSelector withObject:(id)object;
3碱工、 - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
三個方法均為同步執(zhí)行,與線程無關(guān)奏夫,在主線程和子線程中均可調(diào)用怕篷。等同于直接調(diào)用該方法。
與直接調(diào)用方法區(qū)別:直接調(diào)用編譯時會自動校驗(yàn)酗昼。performSelector在運(yùn)行時去找方法廊谓,在編譯時不做校驗(yàn)。
3麻削、在NSRunloop.h中的方法
1蒸痹、- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)modes;
2、- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
3.1呛哟、這兩個方法為異步執(zhí)行叠荠,delay值為0,仍為異步執(zhí)行扫责。
1榛鼎、在主線程執(zhí)行,方法調(diào)用成功。
2者娱、在子線程執(zhí)行抡笼,需要開啟子線程Runloop,方法才可以調(diào)用成功黄鳍。
注意:調(diào)用該方法之前或在該方法所在的VC釋放時主動調(diào)用取消函數(shù)推姻,以確保不會內(nèi)存泄露。
3.2际起、可控制延遲調(diào)用方法的取消操作:
1拾碌、+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
2、+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
4街望、在NSThread.h中的方法
4.1校翔、在主線程調(diào)用方法
1、- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray *)array;
2灾前、- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
在主線程和子線程中均可執(zhí)行防症,都會在主線程調(diào)用Selector方法。
4.1.1哎甲、在子線程中設(shè)置wait值
wait=YES:當(dāng)前線程被阻塞蔫敲,主線程執(zhí)行完Selector,接著執(zhí)行炭玫。
-(void)viewDidLoad{[superviewDidLoad];dispatch_queue_t queue=dispatch_queue_create("cwwng-queue",DISPATCH_QUEUE_CONCURRENT);dispatch_async(queue,^{NSLog(@"1");[selfperformSelectorOnMainThread:@selector(testAction)withObject:nil waitUntilDone:YES];NSLog(@"2");});}-(void)testAction{sleep(3);NSLog(@"testAction");}2020-11-2120:26:12.252984+0800CwwngDemo[28225:462843]12020-11-2120:26:15.289730+0800CwwngDemo[28225:462744]testAction2020-11-2120:26:15.289991+0800CwwngDemo[28225:462843]2
wait=NO:當(dāng)前線程不被阻塞奈嘿。
-(void)viewDidLoad{[superviewDidLoad];dispatch_queue_t queue=dispatch_queue_create("cwwng-queue",DISPATCH_QUEUE_CONCURRENT);dispatch_async(queue,^{NSLog(@"1");[selfperformSelectorOnMainThread:@selector(testAction)withObject:nil waitUntilDone:NO];NSLog(@"2");});}-(void)testAction{sleep(3);NSLog(@"testAction");}2020-11-2120:28:50.383884+0800CwwngDemo[28347:465953]12020-11-2120:28:50.384127+0800CwwngDemo[28347:465953]22020-11-2120:28:53.410808+0800CwwngDemo[28347:465859]testAction
4.1.2、在主線程中設(shè)置wait值
wait=YES:等待Selector執(zhí)行完吞加,再接著執(zhí)行裙犹。
-(void)viewDidLoad{[superviewDidLoad];NSLog(@"1");[selfperformSelectorOnMainThread:@selector(testAction)withObject:nil waitUntilDone:YES];NSLog(@"2");}-(void)testAction{sleep(3);NSLog(@"testAction");}2020-11-2120:33:53.966768+0800CwwngDemo[28608:472716]12020-11-2120:33:56.967413+0800CwwngDemo[28608:472716]testAction2020-11-2120:33:56.967738+0800CwwngDemo[28608:472716]2
wait=NO:不等待Selector執(zhí)行完,接著執(zhí)行衔憨。
-(void)viewDidLoad{[superviewDidLoad];NSLog(@"1");[selfperformSelectorOnMainThread:@selector(testAction)withObject:nil waitUntilDone:NO];NSLog(@"2");}-(void)testAction{sleep(3);NSLog(@"testAction");}2020-11-2120:38:55.042160+0800CwwngDemo[28838:477847]12020-11-2120:38:55.042508+0800CwwngDemo[28838:477847]22020-11-2120:38:58.087345+0800CwwngDemo[28838:477847]testAction
4.2叶圃、在指定線程調(diào)用方法
(void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
(void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
4.3、開啟子線程在后臺運(yùn)行
(void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
作者:Cwwng
鏈接:http://www.reibang.com/p/8db250c1a566
來源:簡書
著作權(quán)歸作者所有践图。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)掺冠,非商業(yè)轉(zhuǎn)載請注明出處。