0

Yes, there are other questions out there about this but I cannot find my answer, so I'm asking it too. Here is my code:

#pragma mark - Timer methods

- (void)counter
{
    self.seconds++;
    double time = self.seconds;
    NSLog(@"seconds = %f", time);
}

- (void)startTimer
{
    self.seconds = 0;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(counter)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)stopTimer:(CaptureTimeType)timerType
{
    [self.timer invalidate];
    self.timer = nil;
}

I went through a tutorial and this code works in a project all it's own. But when I add it to my "real" project, the selector (counter) never gets executed. Does anybody have a clue why this code would work in one project and not in another? Because I'm baffled.

Thank you, Lucy

OK. Here is the code that calls it. It is in the same class and, as far as I know, it is in the same thread:

- (void)generateImage:(cv::Mat)src
{
    [self startTimer];
    ph::ImageProcessor newImage = *([self.processor generateImage:src]);
    [self stopTimer:TEMPLATE];
}

Does this help?

Thanks again.

Patricia
  • 5,019
  • 14
  • 72
  • 152
  • 1
    Threading is your issue, after you call startTimer you need to start your processor on a background thread so the timer can fire in the foreground – Jack Jan 31 '14 at 20:26
  • The startTimer and the stopTimer methods get executed. Only the counter method is skipped. – Patricia Jan 31 '14 at 20:26
  • 1
    Also, if you just want to log the runtime, you should do it like this:startTime = [NSDate date]; NSTimeInterval scanDuration = -[startTime timeIntervalSinceNow]; – Jack Jan 31 '14 at 20:27
  • OK, Jack Wu. I'll try these. But I have one question: Why did it work in a test application? – Patricia Jan 31 '14 at 20:41
  • 1
    Can you elaborate on this "test application" Do you mean running on simulator? – Jack Jan 31 '14 at 20:44
  • Yes. I do. Yes, I think I get it. :-) – Patricia Jan 31 '14 at 20:59
  • What calls `startTimer`? If it is called from any thread of execution that does not have a running run loop, then the timer will never fire. Without seeing more code (like where `startTimer` is called), it is hard to say more. – bbum Jan 31 '14 at 20:16
  • Please see updated question. The CaptureTimeType parameter in stopTimer is no longer used. I need to remove it. – Patricia Jan 31 '14 at 20:31

2 Answers2

1

Your

ph::ImageProcessor newImage = *([self.processor generateImage:src]);

Call is occupying the main thread so your time won't receive updates.

To solve this, you can either

Run you processor in the background (you can use dispatch_async)

Or, if you only want to log the time of your function, you should do it like this:

- (void)generateImage:(cv::Mat)src
{
    NSDate * date = [NSDate date];
    ph::ImageProcessor newImage = *([self.processor generateImage:src]);
    float timePassed = -[date timeIntervalSinceNow];
}
Jack
  • 16,677
  • 8
  • 47
  • 51
  • AWESOME!!! Yeah. All I wanted was the time it took to process. I didn't even know that option was available. Thanks, Jack! :-) – Patricia Jan 31 '14 at 20:51
0

See How do I use NSTimer?

In short: The method called when the timer is fired needs a special signature

-(void)methodName:(NSTimer*)timer

and thus the call to start the timer is like:

[NSTimer scheduledTimerWithTimeInterval:1.0
                                              target:self
                                            selector:@selector(methodName:)
                                            userInfo:nil
                                             repeats:YES];
Community
  • 1
  • 1
Volker
  • 4,640
  • 1
  • 23
  • 31