利用RunLoop来监控APP的卡顿

我们要怎么利用RunLoop的原理来帮我们定位到APP里面的卡顿场景呢?
人善,话不多,拿起键盘就开干。

从以下代码我们可以看出RunLoop的状态有六种,分别是进入RunLoop、处理timer之前、处理source之前、即将进入休眠、唤醒之后、退出RunLoop

    /* Run Loop Observer Activities */
    public struct CFRunLoopActivity : OptionSet {

        public init(rawValue: CFOptionFlags)
        public static var entry: CFRunLoopActivity { get } //进入RunLoop
        public static var beforeTimers: CFRunLoopActivity { get } //处理timer之前
        public static var beforeSources: CFRunLoopActivity { get } //处理source之前
        public static var beforeWaiting: CFRunLoopActivity { get } //即将进入休眠
        public static var afterWaiting: CFRunLoopActivity { get } //唤醒之后
        public static var exit: CFRunLoopActivity { get } //退出RunLoop
        public static var allActivities: CFRunLoopActivity { get } //所有状态
    }

我们可以通过beforeSources和afterWaiting这两个状态来监听卡顿,具体设置代码如下

    func startMonitor() {
        if self.observer == nil {
            self.start = true
            self.semaphore = DispatchSemaphore.init(value: 1)
            //启动一条子线程来监听卡顿
            Thread.detachNewThreadSelector(#selector(subThread), toTarget: self, with: nil)
                        
            //创建观察者
            var context = CFRunLoopObserverContext.init(version: 0, info: nil, retain: nil, release: nil, copyDescription: nil)
            self.observer = CFRunLoopObserverCreate(kCFAllocatorDefault, CFRunLoopActivity.allActivities.rawValue, true, 0, {(observer, activity, pointer) in
                //RunLoop状态改变的回调               			MonitorManager.manager.RunLoopActivity = activity
                MonitorManager.manager.semaphore?.signal()
            }, &context)
            //将观察者添加在主线程RunLoop的commonModes模式下面
            CFRunLoopAddObserver(CFRunLoopGetMain(), self.observer, CFRunLoopMode.commonModes)
        }
    }

    @objc func subThread() {
        while self.start! {
            //这里设置的阈值为500毫秒。阈值不宜设置的太小,太小会检测到一些用户无感的卡顿。也不宜设置的太大,太大了则会错过一些明显的卡顿
            let wait = self.semaphore?.wait(timeout: .now() + 0.5)
            if wait == DispatchTimeoutResult.timedOut {
                if self.observer != nil {
                    if (self.RunLoopActivity == CFRunLoopActivity.beforeSources || self.RunLoopActivity == CFRunLoopActivity.afterWaiting) && self.start == true {
                        /*
                         这里检测到卡顿,可以打印堆栈信息查看是在调用哪些方法的时候导致了卡顿。也可以利用PLCrashReporter这个第三方库来协助我们打印堆栈信息。我们需要把堆栈信息发送到服务器以便分析问题
                         */
                        print("卡顿了")
                    }
                }
            }
        }
        print("子线程销毁了")
    }

来说一下阈值的取值范围。阈值不宜设置的太小,太小会检测到一些用户无感的卡顿。也不宜设置的太大,太大了则会错过一些明显的卡顿。
个人认为,如果想把APP的流畅度优化到很好可以把阈值设置为毫秒级别的,如果只想解决用户能明显感知到的卡顿可以把阈值设置为秒级别的。(这里只是个人的观点,如果有误还望指出)

接下来我们来看一下运行的效果,在UITableViewCell滚动到50行的时候让程序进入睡眠1秒来模拟卡顿,结果如下图所示
在这里插入图片描述
如你所见,在滚动到50行的时候检测出了卡顿。

好了,这次的文章到这里就结束了。如果有说的不当的地方还望各路大神指出。
最后附上GitHubDemo

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值