How to Use nsunloop

Source: Internet
Author: User

Today, I suddenly realized that nstimer is a running method that is implemented in multiple threads or in the main thread. Of course, it cannot be as simple as while in the main thread, so nothing can be done. Simply put, nstimer is running in synchronous mode. The time is up. After the message is sent, the ontimer function is called on the main thread.

We often see this Code:

  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. [Nsunloop currentrunloop] runmode: nsdefaultrunloopmode beforedate: [nsdate distantfuture];
  8. }
  9. [Progress sethidden: Yes];
  10. }

Copy code

This piece of code is amazing because it will "Pause" the code and the program running will not be affected by a while loop. After [Progress sethidden: No] is executed, the entire function is paused and stops in the loop. After the operations in loadpageinbackground are completed, the [Progress
Sethidden: Yes] Run. This is a brief introduction and the logic is clear. If you do not do this, you need to call [Progress sethidden: Yes] In the loadpageinbackground to indicate that the load is complete. It seems that the Code is not compact and error-prone.
[Igoogle: the main thread of the application framework has encapsulated
Runmode: beforedate: Call; it and the while loop constitute a message pump, constantly get and process messages; it may be strange to everyone, since the main thread has encapsulated calls to the nsunloop, why can we call it again? This is the difference between it and Windows message loop. It can be called nested. when the while + nsunloop is called again, the program does not stop execution, and the program continuously extracts or processes messages. this is the same as the nested call of active scheduler in Symbian to achieve synchronization.]

Record a post: http://www.cocoachina.com/bbs/read.php? Tid-38499.html


Another example is as follows:

-(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]);

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.