對象方法 (位置 NSObject.h)
- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
對象category Runloop定時器方法 (位置 NSRunLoop.h 中的NSObject (NSDelayedPerforming))
注意 : 這是runloop定時器方法, 基于runloop, 在主線程上有一條在循環(huán)的runloop,所以在主線程可以直接執(zhí)行, 但是, 但在子線程時, 無法執(zhí)行, 需要獲取當前子線程的runloop, 讓runloop跑起來, 方法才會執(zhí)行
不信可以嘗試下, 在子線程中使用- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;看看會不會執(zhí)行, 如果不執(zhí)行, 在后面加上[[NSRunLoop currentRunLoop] run];再試試, 當然事件執(zhí)行完后,記得結(jié)束掉runloop
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSRunLoopMode> *)modes;
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;
+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;
對象category NSThread 方法 (位置 NSThread.h 中的NSObject (NSThreadPerformAdditions))
// 主線程
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array;
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
// 指定線程
- (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));
// equivalent to the first method with kCFRunLoopCommonModes
// 后臺運行
- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
雖然他們都是NSObject的方法或者是分類補充方法媚污,但實際上司澎,是隸屬于不同模塊的