runloop嵌套理解

在iOS中NSRunLoop是一個(gè)對(duì)象歼捏,run是它的一個(gè)對(duì)象方法,runloop嵌套一般指的是在NSRunLoop的run方法中再調(diào)用一次run方法,runloop的run方法內(nèi)部都是開(kāi)啟了一個(gè)while循環(huán),因此在run方法中再調(diào)用一次run方法則相當(dāng)于在一個(gè)while循環(huán)中開(kāi)啟另一個(gè)while循環(huán),內(nèi)層while循環(huán)會(huì)導(dǎo)致外層while循環(huán)的阻塞盟萨。

    BOOL r1_run = YES;
    BOOL r2_run = YES;

    while (r1_run) {
        NSLog(@"---1-");
        while (r2_run) {
            
        }
        NSLog(@"---2-");
    }

另外,在runloop 中了讨,每開(kāi)啟一個(gè)run方法都是在處理runloop 指定mode下的modeItem的事件捻激,如果外層run方法與內(nèi)層run方法運(yùn)行的是在同一mode下,那在外層run方法中沒(méi)來(lái)得及處理的modeItem的事件在進(jìn)入內(nèi)層run方法后可以繼續(xù)被處理前计。

1胞谭、練習(xí)一


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer1 forMode:NSDefaultRunLoopMode];

}

- (void)timerMethod {
    NSLog(@"---1");
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    NSLog(@"---2");
}

2、練習(xí)二:求下列代碼打印順序

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"1");
    });
    [[NSRunLoop currentRunLoop] performBlock:^{
        NSLog(@"2");
    }];
    NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:10 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSLog(@"3");
        CFRunLoopStop(CFRunLoopGetCurrent());
    }];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:@"mode1"];
    [[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
    NSLog(@"4");
}

3残炮、練習(xí)三:求下列代碼打印內(nèi)容

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 
    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadStartMethod) object:nil];
    [thread start];
}

- (void)threadStartMethod {
    NSTimer *timer1 = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    NSTimer *timer2 = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerMethod2) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:timer1 forMode:@"mode1"];
    [[NSRunLoop currentRunLoop] addTimer:timer2 forMode:@"mode1"];
    [[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
    
}

- (void)timerMethod {
    NSLog(@"---1");
    NSTimer *timer4 = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(timerMethod4) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer4 forMode:@"mode1"];
    [[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
    NSLog(@"---2");
}

- (void)timerMethod2 {
    NSLog(@"---3");
    NSTimer *timer3 = [NSTimer timerWithTimeInterval:10 target:self selector:@selector(timerMethod3) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer3 forMode:@"mode1"];
    [[NSRunLoop currentRunLoop] runMode:@"mode1" beforeDate:[NSDate distantFuture]];
    NSLog(@"---4");
}

- (void)timerMethod3 {
    NSLog(@"---5");
    //    CFRunLoopStop(CFRunLoopGetCurrent());
}

- (void)timerMethod4 {
    NSLog(@"---6");
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末韭赘,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子势就,更是在濱河造成了極大的恐慌泉瞻,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件苞冯,死亡現(xiàn)場(chǎng)離奇詭異袖牙,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)舅锄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)鞭达,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事畴蹭√谷裕” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵叨襟,是天一觀的道長(zhǎng)繁扎。 經(jīng)常有香客問(wèn)我,道長(zhǎng)糊闽,這世上最難降的妖魔是什么梳玫? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮右犹,結(jié)果婚禮上提澎,老公的妹妹穿的比我還像新娘。我一直安慰自己念链,他們只是感情好盼忌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著钓账,像睡著了一般碴犬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上梆暮,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音绍昂,去河邊找鬼啦粹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛窘游,可吹牛的內(nèi)容都是我干的唠椭。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼忍饰,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼贪嫂!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起艾蓝,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤力崇,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后赢织,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體亮靴,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年于置,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茧吊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖搓侄,靈堂內(nèi)的尸體忽然破棺而出瞄桨,到底是詐尸還是另有隱情,我是刑警寧澤讶踪,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布芯侥,位于F島的核電站,受9級(jí)特大地震影響俊柔,放射性物質(zhì)發(fā)生泄漏筹麸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一雏婶、第九天 我趴在偏房一處隱蔽的房頂上張望物赶。 院中可真熱鬧,春花似錦留晚、人聲如沸酵紫。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)奖地。三九已至,卻和暖如春赋焕,著一層夾襖步出監(jiān)牢的瞬間参歹,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工隆判, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留犬庇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓侨嘀,卻偏偏與公主長(zhǎng)得像臭挽,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子咬腕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354

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