NSRunloop 的用法

來源:互聯網
上載者:User

今天突然才之間才意識到NSTimer這樣的運行方式,是在多線程中實現的迴圈還是在主線程中去實現的呢。當然不可能是在主線程中的while那麼簡單,那樣什麼都幹不了,簡單看了下NSTimer是以同步方式啟動並執行。時間到了,訊息發出後,ontimer的函數是在主線程上調用的。

我們會經常看到這樣的代碼:

  1. - (IBAction)start:(id)sender  
  2. {  
  3. pageStillLoading = YES;  
  4. [NSThread detachNewThreadSelector:@selector(loadPageInBackground:)toTarget:self withObject:nil];  
  5. [progress setHidden:NO];  
  6. while (pageStillLoading) {  
  7. [NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];  
  8. }  
  9. [progress setHidden:YES];  
  10. }  

複製代碼

這段代碼很神奇的,因為他會“暫停”代碼運行,而且程式運行不會因為這裡有一個while迴圈而受到影響。在[progress setHidden:NO]執行之後,整個函數想暫停了一樣停在迴圈裡面,等loadPageInBackground裡面的操作都完成了以後才讓[progress
setHidden:YES]運行。這樣做就顯得簡介,而且邏輯很清晰。如果你不這樣做,你就需要在loadPageInBackground裡面表示load完成的地方調用[progress setHidden:YES],顯得代碼不緊湊而且容易出錯。
[iGoogle有話說:應用程式架構主線程已經封裝了對NSRunLoop
runMode:beforeDate:的調用;它和while迴圈構成了一個訊息泵,不斷擷取和處理訊息;可能大家會比較奇怪,既然主線程中已經封裝好了對NSRunLoop的調用,為什麼這裡還可以再次調用,這個就是它與Windows訊息迴圈的區別,它可以嵌套調用.當再次調用while+NSRunLoop時候程式並沒有停止執行,它還在不停提取訊息/處理訊息.這一點與Symbian中Active Scheduler的嵌套調用達到同步作用原理是一樣的.]

記錄一篇文章:http://www.cocoachina.com/bbs/read.php?tid-38499.html


還有一個使用的例子:

-(void)backgroundThreadStarted {

  NSAutoreleasePool* thePool = [[NSAutoreleasePool alloc] init];

  // create a scheduled timer

  NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES];

  [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

  m_isBackgroundThreadToTerminate = NO;

  // create the runloop

  double resolution = 300.0;

  BOOL isRunning;

  do {

    // run the loop!

    NSDate* theNextDate = [NSDate dateWithTimeIntervalSinceNow:resolution]; 

    isRunning = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:theNextDate]; 

    // occasionally re-create the autorelease pool whilst program is running

    [thePool release];

    thePool = [[NSAutoreleasePool alloc] init];            

  } while(isRunning==YES && m_isBackgroundThreadToTerminate==NO);

  

  [thePool release];

}

-(void)backgroundThreadFire:(id)sender {

  // do repeated work every one second here

  // when thread is to terminate, call [self backgroundThreadTerminate];

}

-(void)backgroundThreadTerminate {

  m_isBackgroundThreadToTerminate = YES;

  CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.