如何在iOS中暂停和停止计算

时间:2015-09-15 01:00:38

标签: ios objective-c

我有一些计算,我将在代码中向您展示。我知道如何开始这个计算,但我不知道如何在计算过程中暂停和停止让我编写代码,以便您理解。

.m文件

@interface ViewController (){
float f;
}
@property double result;
@end

 @implementation ViewController
 @synthesize textLabel;
 @synthesize TimeLabel;

 -(void)calculatingPIValue{
 NSDate *methodStart = [NSDate date];
    int r[2800 + 1];
    int i,k;
    int b;
    int d;
    double c = 0;
    for ( i = 0; i < 2800; i++) {

        r[i] = 20000; }
    for ( k = 2800; k > 2790; k -= 14){

        d = 0;
        i = k;
        for (;;) {
            d += r[i] * 10000;

            b = 2 * i - 1;
            r[i] = d % b;
            d /= b;
            i--;
            if (i == 0) break;
            d *= i;
        }
        f = ((c + d) / 10000000)/10;
         NSLog(@"the value is %.09f",f);
    }
    self.textLabel.text = [NSString stringWithFormat:@"%.08f",f];
    NSDate *methodFinish = [NSDate date];
    NSTimeInterval executionTime = [methodFinish      timeIntervalSinceDate:methodStart];
    NSLog(@"executionTime = %f", executionTime);
    TimeLabel.text = [NSString stringWithFormat:@"%f",executionTime];

    });
- (IBAction)startButtonPressed:(UIButton *)sender{

  [self calculatingPIValue];


  }


  - (IBAction)pauseButton:(UIButton *)sender {
  }

  - (IBAction)stopButton:(UIButton *)sender {
  }
  @end

1 个答案:

答案 0 :(得分:1)

您需要重构此代码才能在后台运行。我建议使用GCD和dispatch_async。您可以编写它来检查布尔实例变量(abort)的值,并在stop == YES时返回。

这样的事情:

@interface ViewController (){
float f;
}
//---------------------------------
//This is new
@property (atomic, assign) BOOL abort; 
//---------------------------------
@property double result;
@end

 @implementation ViewController
 @synthesize textLabel;
 @synthesize TimeLabel;


 -(void)calculatingPIValue
  {
    NSDate *methodStart = [NSDate date];
    int r[2800 + 1];
    int i,k;
    int b;
    int d;
    double c = 0;
    for ( i = 0; i < 2800; i++) {

        r[i] = 20000; }
    for ( k = 2800; k > 2790; k -= 14){

        d = 0;
        i = k;
        for (;;) {
            //---------------------------------
            //This is the new code
            if (self.abort)
              return;
            //---------------------------------
            d += r[i] * 10000;

            b = 2 * i - 1;
            r[i] = d % b;
            d /= b;
            i--;
            if (i == 0) break;
            d *= i;
        }
        f = ((c + d) / 10000000)/10;
         NSLog(@"the value is %.09f",f);
    }
    self.textLabel.text = [NSString stringWithFormat:@"%.08f",f];
    NSDate *methodFinish = [NSDate date];
    NSTimeInterval executionTime = [methodFinish      timeIntervalSinceDate:methodStart];
    NSLog(@"executionTime = %f", executionTime);
    TimeLabel.text = [NSString stringWithFormat:@"%f",executionTime];

  }

- (IBAction)startButtonPressed:(UIButton *)sender
  {
    //---------------------------------
    //This code is changed
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH), ^
    {
      [self calculatingPIValue];
    });
    //---------------------------------
  }


  - (IBAction)pauseButton:(UIButton *)sender {
  }

  - (IBAction)stopButton:(UIButton *)sender 
  {
     self.abort = TRUE;
  }
  @end

这是基本的想法。暂停然后恢复计算将会复杂得多。