开发者

How can I make a method stall for a fixed amount of time?

I have an app that calls a sometimes-fast, sometimes-slow method. I know an upper bound for how long it will take (2 seconds). I'd like to set a timer to start when the method is called, run the code, but then not produce the output until 2 seconds has passed, no matter how long it actually takes. That way the开发者_开发知识库 user perceives the action as always taking the same amount of time. How can I implement this?

What I would like is something along the lines of this:

-(IBAction)doStuff {

  // START A TIMER, LOOK BUSY
  [activityIndicator startAnimating];

  ... real work happens here ...
  ... NSString *coolString gets assigned ...

  // WHEN TIMER == 2 SECONDS, REVEAL COOLNESS
  [activityIndicator stopAnimating];
  [textField setText:coolString];

}


There are a couple of ways to delay an action in Cocoa. The easiest may be to use performSelector:withObject:afterDelay:. This method sets up a timer for you and calls the specified method when the time comes. It's an NSObject method, so your objects all get it for free.

The tricky part here is that the first method will block the main thread, so you need get it onto a background thread, and then get back to the main thread in order to update the UI. Here's a stab at it:

// Put the method which will take a while onto another thread
[self performSelectorInBackground:@selector(doWorkForUnknownTime)
                       withObject:nil];
// Delay the display for exactly two seconds, on the main thread
[self performSelector:@selector(displayResults)
           withObject:nil
           afterDelay:2.0];

- (void)doWorkForUnknownTime {

   // results is an ivar
   results = ...; // Perform calculations
}

- (void)displayResults {
    if( !results ){
        // Make sure that we really got results
        [self performSelector:@selector(displayResults:)
                   withObject:nil
                   afterDelay:0.5];
        return;
    }

    // Do the display!
}

The only other thing I can think of is to store the time that the "work" method is called in an NSDate, and check how long it took when you get the results. If it isn't two seconds yet, sleep the background thread, then call back to the main thread when you're done.

[self performSelectorInBackground:@selector(doWorkForUnknownTime:)
                       withObject:[NSDate date]];

- (void)doWorkForUnknownTime:(NSDate *)startTime {

   // All threads must have an autorelease pool in place for Cocoa.
   @autoreleasepool{
       // This will take some time
       NSString * results = ...; // Perform calculations

       NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startTime];
       if( elapsedTime < 2.0 ){
           // Okay to do this to wait since we're on a background thread, 
           // although not ideal; sleeping threads are kind of wasteful.
           // Try not to do this a lot.
           sleep(2.0 - elapsedTime);
       }

       // Don't forget to retain results on the main thread!
       [self performSelectorOnMainThread:@selector(displayResults:)
                              withObject:results
                           waitUntilDone:YES];
//     [results release];    // if necessary
    }
}


[self performSelector:@selector(myfunc) withObject: afterDelay:];  

should help.


-(IBAction)doStuff {

  // START A TIMER, LOOK BUSY
  [activityIndicator startAnimating];

  ... real work happens here ...
  ... NSString *coolString gets assigned ...

  // WHEN TIMER == 2 SECONDS, REVEAL COOLNESS
[self performSelector:@selector(revealCoolnessWithString:) withObject:coolString afterDelay:2];

}

- (void)revealCoolnessWithString:(NSString *)coolString
{
[activityIndicator stopAnimating];
[textField setText:coolString];
}

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜