zoukankan      html  css  js  c++  java
  • performSelector的方法

    在此我对performSelector系列方法进行了总结

    1、

    - (id)performSelector:(SEL)aSelector;

    - (id)performSelector:(SEL)aSelector withObject:(id)object;

    - (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

    这三个方法都是同步执行,与线程无关,在需要动态的去调用方法的时候去使用。

    例如:

    [self performSelector:@selector(configUI)]; 与[self configUI]; 效果完全相同。

    withObject:(id)object 这是要传递的参数

    2、

    - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay inModes:(NSArray<NSString *> *)modes;

    - (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;

      这两个方法为异步执行,只能在主线程中执行。可用于点击UI中一个按钮会触发一个消耗性能的事件,在事件执行期间按钮会一直处于高亮状态,此时可以调用该方法去异步的处理该事件,避免上述问题。

    在方法未到执行时间之前,取消方法为:

    + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument;

    + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget;

    调用该方法之前或在该方法所在的viewController生命周期结束的时候去调用取消函数,以确保不会引起内存泄露。

    3、

    - (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;

    这两个方法,在主线程和子线程中均可执行,均会调用主线程的aSelector方法

    如果设置wait为YES:等待当前线程执行完以后,主线程才会执行aSelector方法;

    设置为NO:不等待当前线程执行完,就在主线程上执行aSelector方法。

    如果,当前线程就是主线程,那么aSelector方法会马上执行。

    注意:apple不允许程序员在主线程以外的线程中对ui进行操作,此时我们必须调用performSelectorOnMainThread函数在主线程中完成UI的更新

    4、 

    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait modes:(nullable NSArray<NSString *> *)array NS_AVAILABLE(10_5, 2_0);

    - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait NS_AVAILABLE(10_5, 2_0);

    在我们指定的线程中调用方法。

    5、

    - (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg NS_AVAILABLE(10_5, 2_0);

    后台执行

  • 相关阅读:
    PHPNow升级PHP版本为5.3.5的方法(转)
    常用Raspberry Pi周边传感器的使用教程(转)
    Raspberry pi 使用python+pySerial实现串口通信(转)
    树莓派相关-树莓派串口配置方法(转)
    树莓派折腾---红外探测
    String.format和MessageFormat.format的对比用法
    使用FastJson从json串中根据key获取value
    使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
    gradle查看依赖关系并写入到文本文件的命令
    使用 "java -jar"命令启动jar包时报不支持的jdk版本异常
  • 原文地址:https://www.cnblogs.com/shifenglin/p/5329595.html
Copyright © 2011-2022 走看看