iOS底層 -- RunLoop之相關(guān)應(yīng)用

RunLoop在實(shí)際開中的應(yīng)用
  • 解決NSTimer在滑動(dòng)時(shí)停止工作的問題
  • 控制線程生命周期(線程保活)
  • 監(jiān)控應(yīng)用卡頓
  • 性能優(yōu)化
一列肢、解決NSTimer在滑動(dòng)時(shí)停止工作的問題
  • 在拖拽時(shí)定時(shí)器不工作
__block int count = 0;

// 默認(rèn)添加到default模式
[NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
    NSLog(@"%d", ++count);
}];

打印結(jié)果

  • 在拖拽時(shí)人仍然正常工作
static int count = 0;
// 2.添加到指定模式下
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
    NSLog(@"%d", ++count);
}];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

// NSDefaultRunLoopMode建车、UITrackingRunLoopMode才是真正存在的模式
// NSRunLoopCommonModes并不是一個(gè)真的模式促王,它只是一個(gè)標(biāo)記
// timer能在_commonModes數(shù)組中存放的模式下工作
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

二 線程甭鞔螅活

我們先封裝一個(gè)長(zhǎng)久活命的線程

  • PermanentThread.h
// 聲明一個(gè)block - 用于執(zhí)行任務(wù)
typedef void(^PermanentThreadTask)(void);

/** 線程毕德澹活 */
@interface PermanentThread : NSObject

// 在當(dāng)前線程執(zhí)行一個(gè)任務(wù)
- (void)executeTask:(PermanentThreadTask)task;

// 結(jié)束線程
- (void)stop;

@end

  • PermanentThread.m
/** CSThread **/
@interface CSThread : NSThread
@end
@implementation CSThread
- (void)dealloc {
    NSLog(@"%s", __func__);
}
@end

@interface PermanentThread()
/** 線程*/
@property(nonatomic,strong)CSThread *thread;
/** 是否停止*/
@property(nonatomic,assign, getter=isStopped)BOOL stopped;
@end

@implementation PermanentThread

// 初始化方法
- (instancetype)init {
    self = [super init];
    if (self) {
        self.stopped = NO;

        // 初始化線程
        __weak typeof(self) weakSelf = self;
        self.thread = [[CSThread alloc] initWithBlock:^{
            // runloop只有添加事件才會(huì)執(zhí)行
            [[NSRunLoop currentRunLoop] addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];

            // 當(dāng)當(dāng)前對(duì)象存在并且變量為false的時(shí)候,才一直執(zhí)行
            while (weakSelf && !weakSelf.isStopped) {
                [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
            }
        }];

        // 開啟線程
        [self.thread start];
    }
    return self;
}

- (void)dealloc {
    NSLog(@"%s", __func__);

    [self stop];
}

#pragma mark - public method

// 執(zhí)行任務(wù)
- (void)executeTask:(PermanentThreadTask)task {
    // 如果線程釋放或者無任務(wù),則退出
    if (!self.thread || !task) {
        return;
    }

    // 開始執(zhí)行任務(wù)
    [self performSelector:@selector(innerExecuteTask:) onThread:self.thread withObject:task waitUntilDone:NO];
}

// 停止
- (void)stop {
    if (!self.thread) {
        return;
    }

    [self performSelector:@selector(innerStop) onThread:self.thread withObject:nil waitUntilDone:YES];
}

#pragma mark - private method

// 執(zhí)行任務(wù)
- (void)innerExecuteTask:(PermanentThreadTask)task {
    task();
}

// 停止線程 runloop
- (void)innerStop {
    self.stopped = YES;
    CFRunLoopStop(CFRunLoopGetCurrent());
    self.thread = nil;
}

@end

外部調(diào)用

- (void)viewDidLoad {
    [super viewDidLoad];

    // 2.線程笨⌒裕活
    self.thread = [[PermanentThread alloc] init];
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.thread executeTask:^{
        NSLog(@"執(zhí)行任務(wù) - %@", [NSThread currentThread]);
    }];
}

- (void)stopBtnClick {
    [self.thread stop];
}

運(yùn)行結(jié)果

三、讓UITableView描扯、UICollectionView延遲加載圖片

首先創(chuàng)建一個(gè)單例定页,單例中定義了幾個(gè)數(shù)組,用來存要在runloop循環(huán)中執(zhí)行的任務(wù)绽诚,然后為主線程的runloop添加一個(gè)CFRunLoopObserver,當(dāng)主線程在NSDefaultRunLoopMode中執(zhí)行完任務(wù)典徊,即將睡眠前杭煎,執(zhí)行一個(gè)單例中保存的一次圖片渲染任務(wù)。關(guān)鍵代碼看 RunLoopWorkDistribution即可卒落。

四羡铲、監(jiān)測(cè)主線程的卡頓,并將卡頓時(shí)的線程堆棧信息保存下來儡毕,選擇合適時(shí)機(jī)上傳到服務(wù)器

第一步也切,創(chuàng)建一個(gè)子線程,在線程啟動(dòng)時(shí)腰湾,啟動(dòng)其RunLoop雷恃。

+ (instancetype)shareMonitor
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init];
        instance.monitorThread = [[NSThread alloc] initWithTarget:self selector:@selector(monitorThreadEntryPoint) object:nil];
        [instance.monitorThread start];
    });

    return instance;
}

+ (void)monitorThreadEntryPoint
{
    @autoreleasepool {
        [[NSThread currentThread] setName:@"FluencyMonitor"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}

第二步,在開始監(jiān)測(cè)時(shí)檐盟,往主線程的RunLoop中添加一個(gè)observer褂萧,并往子線程中添加一個(gè)定時(shí)器,每0.5秒檢測(cè)一次耗時(shí)的時(shí)長(zhǎng)葵萎。

- (void)start
{
    if (_observer) {
        return;
    }

    // 1.創(chuàng)建observer    CFRunLoopObserverContext context = {0,(__bridge void*)self, NULL, NULL, NULL};

    _observer = CFRunLoopObserverCreate(kCFAllocatorDefault,
                                              kCFRunLoopAllActivities,
                                              YES,
                                              0,
                                              &runLoopObserverCallBack,
                                              &context);

    // 2.將observer添加到主線程的RunLoop中 
    CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);

    // 3.創(chuàng)建一個(gè)timer导犹,并添加到子線程的RunLoop中    
    [self performSelector:@selector(addTimerToMonitorThread) onThread:self.monitorThread withObject:nil waitUntilDone:NO modes:@[NSRunLoopCommonModes]];

}

- (void)addTimerToMonitorThread
{
    if (_timer) {
        return;
    }

    // 創(chuàng)建一個(gè)timer   
    CFRunLoopRef currentRunLoop = CFRunLoopGetCurrent();

    CFRunLoopTimerContext context = {0, (__bridge void*)self, NULL, NULL, NULL};

    _timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.1, 0.01, 0, 0, &runLoopTimerCallBack, &context);

    // 添加到子線程的RunLoop中  
    CFRunLoopAddTimer(currentRunLoop, _timer, kCFRunLoopCommonModes);
}

第三步,補(bǔ)充觀察者回調(diào)處理

static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){

    FluencyMonitor *monitor = (__bridge FluencyMonitor*)info;

    NSLog(@"MainRunLoop---%@",[NSThread currentThread]);

    switch (activity) {

        case kCFRunLoopEntry:

            NSLog(@"kCFRunLoopEntry");

            break;

        case kCFRunLoopBeforeTimers:

            NSLog(@"kCFRunLoopBeforeTimers");

            break;

        case kCFRunLoopBeforeSources:

            NSLog(@"kCFRunLoopBeforeSources");

            monitor.startDate = [NSDate date];

            monitor.excuting = YES;

            break;

        case kCFRunLoopBeforeWaiting:

            NSLog(@"kCFRunLoopBeforeWaiting");

            monitor.excuting = NO;

            break;

        case kCFRunLoopAfterWaiting:

            NSLog(@"kCFRunLoopAfterWaiting");

            break;

        case kCFRunLoopExit:

            NSLog(@"kCFRunLoopExit");

            break;

        default:

            break;

    }

}

RunLoop進(jìn)入睡眠狀態(tài)的時(shí)間可能會(huì)非常短羡忘,有時(shí)候只有1毫秒谎痢,有時(shí)候甚至1毫秒都不到,靜止不動(dòng)時(shí)卷雕,則會(huì)長(zhǎng)時(shí)間進(jìn)入睡覺狀態(tài)节猿。

因?yàn)橹骶€程中的block、交互事件漫雕、以及其他任務(wù)都是在kCFRunLoopBeforeSources 到 kCFRunLoopBeforeWaiting 之前執(zhí)行滨嘱,所以我在即將開始執(zhí)行Sources 時(shí),記錄一下時(shí)間浸间,并把正在執(zhí)行任務(wù)的標(biāo)記置為YES太雨,將要進(jìn)入睡眠狀態(tài)時(shí),將正在執(zhí)行任務(wù)的標(biāo)記置為NO魁蒜。

第四步囊扳,補(bǔ)充timer 的回調(diào)處理

static void runLoopTimerCallBack(CFRunLoopTimerRef timer, void *info)
{
    FluencyMonitor *monitor = (__bridge FluencyMonitor*)info;
    if (!monitor.excuting) {
        return;
    }

    // 如果主線程正在執(zhí)行任務(wù),并且這一次loop 執(zhí)行到 現(xiàn)在還沒執(zhí)行完兜看,那就需要計(jì)算時(shí)間差    

    NSTimeInterval excuteTime = [[NSDate date] timeIntervalSinceDate:monitor.startDate];

    NSLog(@"定時(shí)器---%@",[NSThread currentThread]);
    NSLog(@"主線程執(zhí)行了---%f秒",excuteTime);

    if (excuteTime >= 0.01) {
        NSLog(@"線程卡頓了%f秒",excuteTime);
        [monitor handleStackInfo];
    }
}

timer 每 0.01秒執(zhí)行一次锥咸,如果當(dāng)前正在執(zhí)行任務(wù)的狀態(tài)為YES,并且從開始執(zhí)行到現(xiàn)在的時(shí)間大于闕值细移,則把堆棧信息保存下來搏予,便于后面處理。

為了能夠捕獲到堆棧信息弧轧,我把timer的間隔調(diào)的很醒┙摹(0.01)球涛,而評(píng)定為卡頓的闕值也調(diào)的很小(0.01)校镐。 實(shí)際使用時(shí)這兩個(gè)值應(yīng)該是比較大亿扁,timer間隔為1s,卡頓闕值為2s即可鸟廓。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末从祝,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子引谜,更是在濱河造成了極大的恐慌牍陌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件员咽,死亡現(xiàn)場(chǎng)離奇詭異毒涧,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)贝室,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門契讲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人滑频,你說我怎么就攤上這事捡偏。” “怎么了峡迷?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵银伟,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我绘搞,道長(zhǎng)彤避,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任夯辖,我火速辦了婚禮琉预,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘楼雹。我一直安慰自己模孩,他們只是感情好尖阔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布贮缅。 她就那樣靜靜地躺著,像睡著了一般介却。 火紅的嫁衣襯著肌膚如雪谴供。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天桂肌,我揣著相機(jī)與錄音数焊,去河邊找鬼。 笑死崎场,一個(gè)胖子當(dāng)著我的面吹牛跪帝,可吹牛的內(nèi)容都是我干的珠十。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼褐着!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起雁社,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤置济,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后谆扎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挂捅,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年堂湖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了闲先。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡无蜂,死狀恐怖饵蒂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酱讶,我是刑警寧澤退盯,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站泻肯,受9級(jí)特大地震影響渊迁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜灶挟,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一琉朽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧稚铣,春花似錦箱叁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至抬伺,卻和暖如春螟够,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工妓笙, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留若河,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓寞宫,卻偏偏與公主長(zhǎng)得像萧福,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子辈赋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容