-1

I am New in iOS, In my Project ,i am using NSTimer with Navigation Controller. I used two Classes in my project, Class 1 is ViewController and 2nd Class is PlayTheme. ViewController and PlayTheme Classes are connected with segue. In PlayTheme Class i used NSTimer and "FiredTimer method" call for Every 10 miliseconds. Source Code of PlayTheme Class is: Below Method For NSTimer Start

- (IBAction)startTimerMethod:(id)sender
{
    UIBackgroundTaskIdentifier bgTask =0;
        UIApplication  *app = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
            [app endBackgroundTask:bgTask];
        }];

    timer = [NSTimer
             scheduledTimerWithTimeInterval:0.01
             target:self
             selector:@selector(timerFired:)
             userInfo:nil
             repeats:YES];
}

And Below Method is Stop timer

- (IBAction)stopTimerMethod:(id)sender
{
    if([timer isValid])
    {
        [timer invalidate];
        timer=Nil;
    }
}

Both Methods are Working ,but When i follows the Below steps the timer Does Not Stop :

  1. I'm in PlayTheme Class and StartTime
  2. Go Back to ViewController
  3. Come Back To PlayTheme Class
  4. and StopTimer Method Call,Method Call But but Does Not Stop Timer

Give Me suggestion for solving my problem and also tell me how NSTimer work in BackGround to play sound for a specific time using NSTimer ?

Thank You in Advanced.

Jaffer Sheriff
  • 1,444
  • 13
  • 33
Vijay Rathod
  • 197
  • 1
  • 3
  • 11

4 Answers4

0

When you exit the PlayTheme view controller, you lose the reference to the timer. You will need to store the timer somewhere 'globally' if you want to keep a reference to it. How and where you do this depends on the structure of your application.

Edit

To clear up: I do not advise using globals! I merely wrote that to indicate a variable that 'exists' in more places then just the one controller. Check @sweepy_'s answer for a possible solution.

Rengers
  • 14,911
  • 1
  • 36
  • 54
0

Can you please tell how you added the timer?

It should be like this -

[[NSRunLoop mainRunLoop] addTimer: timer forMode:NSRunLoopCommonModes];

Sanjay Mohnani
  • 5,947
  • 30
  • 46
0

Several solutions are available to solve your case:

  • Discouraged - Either store a reference to your NSTimer instance globally (have a look at the Singleton design pattern)
  • Recommended - Instantiate your timer from the first view controller so that you always keep a reference on it, and eventually pass it to your destination PlayTheme view controller.

Always keep in mind that if your have to share an element with several instances, this element must be managed by the first common father of all these instances.

 VC1 -> VC2 -> VC3
 VC1 -> VC5
 // In such a case, if both VC3 and VC5 need to share an element,
 // this one must be managed by VC1
sweepy_
  • 1,333
  • 1
  • 9
  • 28
0

To create a background timer subclass NSOperation and create it's instance. Also to keep timer active for specific duration you need to specify the date from the current time + duration, like this -

#import <Foundation/Foundation.h>

@interface BackgroundTimer : NSOperation
{
    BOOL _done;
}
@end



#import "BackgroundTimer.h"

@implementation BackgroundTimer

-(void) main
{
    if ([self isCancelled])
    {
        return;
    }

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30
                                             target:self
                                           selector:@selector(timerFired)
                                           userInfo:nil
                                            repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    //keep the runloop going as long as needed
    while (!_done && [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                              beforeDate:[NSDate dateTillTheSpecificTime]]);

}

@end

Sanjay Mohnani
  • 5,947
  • 30
  • 46