在自建线程监听主线程一次Runloop时间,用于判断卡顿
Objective-C
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
AYRunLoopWatchDog.xcodeproj
AYRunLoopWatchDog
.gitignore
README.md

README.md

简介

功能

  • 以一次Runloop时间计时,如果超过规定时间,调用外部超时block
  • 有一个ping timer,会在固定时间去检测当前Runloop执行时间,如果连续3次超过规定卡顿时间,则上报一次且在Runloop退出时不会再次上报
  • 当程序退到后台,停止记录
  • watchdog的所有操作均在独立的保活线程上执行,对主线程没有影响

关于RunLoop计时

使用了两个observer,一个order为INT_MIN的observer,由于在observers中调用顺序早,用于记录进入RunLoop的事件;一个order为INT_MAX的observer, 在一个RunLoop靠后的时机去记录出RunLoop的时间,计算整个一个RunLoop的时间。

使用

创建

    self.dog = [[NVRunLoopWatchdog alloc] initWithRunLoop:CFRunLoopGetMain() stallingCallBack:^(NSTimeInterval duration) {
        NSLog(@"################# 主线程出现卡顿,时长 %f", duration);
    }];

开启

    [self.dog startWatching];

停止

    [self.dog stopWatching];