Xcode9 Main Thread Checker 主线程和子线程

http://xuyafei.com/post/public/main-thread-checker

https://www.jianshu.com/p/c6ab011f449f


在子程更新 UI 是一种常见的错误。这会引起 UI 更细失败、显示错误、数据损坏、甚至崩溃。
Main Thread Checker Xcode9 加入的一个独立工具,用来检测使用 Swift 和 C 语言在子线程调用 UIKit、AppKit 等必须在主线程使用的 API。

在 App 启动时,Main Thread Checker 动态的替换了所有只能在主线程调用的方法的实现(已知的在后台线程上可以安全使用的方法不会被替换),替换后的方法带有预先检查当前线程的功能,当发现非法调用时,Xcode 就会断在这行调用中。

不像其他代码诊断工具,Main Thread Checker 不用重新编译并且可以在已有的二进制包上使用。你可以在不使用 Xcode 的情况下在一个 macOS app 上运行 Main Thread Checker。例如在一个持续集成系统上通过注入动态库文件 /Applications/Xcode.app/Contents/Developer/usr/lib/libMainThreadChecker.dylib 来运行它。


性能影响:

Main Thread Checker 的性能影响是很小的, 仅有 1–2% CPU 占用,增加的启动时间 0.1 秒。

因为对性能影响较小,Main Thread Checker 在你用 Xcode 运行 App 时是默认开启的。


解决:

长时间运行的任务(如网络请求)通常都在后台执行,并且提供一个 completion handler 来处理完成后事情。 试图在 completion handler 中获取或更新 UI 可能会导致问题。

这种情况下应该使用 GCD 将调用派发到主线程来解决。


在APP开发中,很多时候我们需要用多线程的方式下载图片,进行网络请求等,在子线程任务完成后需要刷新主线程的UI,呈现结果。下面介绍三种同步代码到主线程的方法:
  • NSThread 方式

查询iOS文档:

@interface 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;
    // equivalent to the first method with kCFRunLoopCommonModes

应用:

- (void)updateUIWith:(UIColor *)color{
    
    [self.bottomView setBackgroundColor:color];
}
[self performSelectorOnMainThread:@selector(updateUIWith:) withObject:[UIColor purpleColor] waitUntilDone:YES  modes:[NSArray arrayWithObject:(__bridge NSString*)kCFRunLoopCommonModes]];

参数说明:

  1. (SEL)aSelector
    刷新主线程调用的方法
  2. withObject:(nullable id)arg
    需要传递的参数
  3. modes:(nullable NSArray<NSString *> *)array
    需要传入的数组对象类型是NSString参数类型,由文档可知modes实际需要的参数定义是这样的:
CF_EXPORT const CFStringRef kCFRunLoopDefaultMode;
CF_EXPORT const CFStringRef kCFRunLoopCommonModes;

因此在使用时需要用桥接的方法进行转换。
modes参数是可选的,如果不用该参数,可以直接用文档中下面的方法,并且文档已给说明,此时modes是kCFRunLoopCommonModes的方式。

  • NSOperationQueue 方式
    NSOperationQueue 是iOS封装的多线程类,可以用类方法直接调用得到主线程
+ (NSOperationQueue *)mainQueue NS_AVAILABLE(10_6, 4_0);

将需要调用的方法直接添加到主线程中即可,用NSOperationQueue封装的对象方法

- (void)addOperationWithBlock:(void (^)(void))block NS_AVAILABLE(10_6, 4_0);

应用

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.bottomView setBackgroundColor:[UIColor redColor]];

        }];
  • GCD 方式
    首先要获取到主线程,文档如下:
/*!
 * @function dispatch_get_main_queue
 *
 * @abstract
 * Returns the default queue that is bound to the main thread.
 *
 * @discussion
 * In order to invoke blocks submitted to the main queue, the application must
 * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
 * thread.
 *
 * @result
 * Returns the main queue. This queue is created automatically on behalf of
 * the main thread before main() is called.
 */
DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_CONST DISPATCH_NOTHROW
dispatch_queue_t
dispatch_get_main_queue(void)
{
    return DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q);
}

将调用的方法绑定到主线程,这里用block的方法,当然也是系统封装的方法,实现代码如下:

dispatch_async(dispatch_get_main_queue(), ^{
           
            [self updateUIWith:[UIColor greenColor]];
            
        });


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值