Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTimer not calling Method [duplicate]

I have an NSTimer that should call a method every second, but it doesn't seem to be calling it at all.

This is how I declare the timer:

[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];

This is the method it calls:

-(void) fadeManager: (NSTimer*) timer{
    NSLog(@"IT'S WORKING!");
    self.playHead += .1; // keeps track of where they are in full track
    if (self.playHead >= self.wait  && ![player isPlaying]) { // checks if wait is over
        [player play];
    }
    if (self.playHead >= self.duration + self.startTime && [player isPlaying]) { // checks if full duration is over
        [player pause];
        [self reset];
    }
    int fadeOutArea = self.startTime + self.duration - self.fadeOut;
    int fadeInArea = self.startTime + self.fadeIn;
    if (self.playHead <= fadeInArea && [player volume] < relativeVolume) { // checks if fadingIn.
        [self fadeInIncriment];
    }
    if (self.playHead >= fadeOutArea  && [player volume] > 0) {
        [self fadeOutIncriment];
    }
}

The code was not working so I put the NSLog in as well as a break point. It seems that it is never being called. Why is this? Does it matter that I declared the method in the .m file like this:

#import <AVFoundation/AVFoundation.h>
@interface CueMusic ()

- (void) delayFadeOut: (AVAudioPlayer*) dFade;
- (void) fadeInIncriment;
- (void) fadeOutIncriment;
- (void) fadeManager: (NSTimer*) timer; // <--------
- (void) start;

@end

@implementation CueMusic
 .......
like image 729
sinθ Avatar asked Mar 20 '13 16:03

sinθ


2 Answers

Either use

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

or

//schedules the timer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fadeManager:) userInfo:nil repeats:YES];

From the docs Scheduling Timers in Run Loops

  • Use the scheduledTimerWithTimeInterval:invocation:repeats: or scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer and schedule it on the current run loop in the default mode.

  • Use the timerWithTimeInterval:invocation:repeats: or timerWithTimeInterval:target:selector:userInfo:repeats: class method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the addTimer:forMode: method of the corresponding NSRunLoop object.)


Swift Code

Either

let timer: NSTimer = NSTimer(timeInterval: 1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)
NSRunLoop.mainRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)

Or

//schedules the timer
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "fadeManager:", userInfo: nil, repeats: true)
like image 147
Inder Kumar Rathore Avatar answered Nov 15 '22 18:11

Inder Kumar Rathore


Your problem is that when using timerWithTimeInterval:target:selector:userInfo:repeats:, the resulting timer does not automatically get added to the run loop. I would recommend using scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: instead, which performs this step for you.

If you prefer to use timerWithTimeInterval:target:selector:userInfo:repeats: then you need to manually add the timer to the current run loop. To do this, call NSRunLoop's addTimer:forMode: method. Documentation

[[NSRunLoop currentRunLoop] addTimer:tTimer forMode: NSDefaultRunLoopMode];
like image 23
FreeAsInBeer Avatar answered Nov 15 '22 20:11

FreeAsInBeer