关于PerformSelector方法

对象方法 (位置 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, 在主线程上有一条在循环的runloop,所以在主线程可以直接执行, 但是, 但在子线程时, 无法执行, 需要获取当前子线程的runloop, 让runloop跑起来, 方法才会执行

不信可以尝试下, 在子线程中使用- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;看看会不会执行, 如果不执行, 在后面加上[[NSRunLoop currentRunLoop] run];再试试, 当然事件执行完后,记得结束掉runloop

  - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray *)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 *)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 *)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的方法或者是分类补充方法,但实际上,是隶属于不同模块的

你可能感兴趣的:(关于PerformSelector方法)