0

I have the NSTimer that needs to be run in the background as you navigate around to different pages, but when I had that in the view controller files, id move to another page and it would essentially destroy the timer. so I made a singleton class and attempted to put the code in there and just call it from there. at this point the code runs, but it doesn't continue to run the timer when I navigate to another page. any ideas are greatly appreciated, please ask for more info!

Code: Viewcontroller.h

#import <UIKit/UIKit.h>
#import "ApplicationManager.h"
@interface ViewController : UIViewController{

IBOutlet UILabel *time;
NSTimer *ticker;
}

- (IBAction)start;
- (IBAction)reset;


- (void)showActivity;


@end
//
//  ViewController.m
//  License
//
//  Created by Connor Gosell on 7/2/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//

#import "ViewController.h"
#import "ApplicationManager.h"
@interface ViewController ()

@end

@implementation ViewController

-(IBAction) start
{
ticker:[NSTimer scheduledTimerWithTimeInterval:1.0 target:self         selector:@selector(showActivity) userInfo:nil repeats:YES];
}

-(IBAction)reset
{
[ticker invalidate];
time.text = @" 0:00";
}

-(void) showActivity
{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}




- (void)viewDidLoad
{
[super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
/*-(IBAction) start
{
[[ApplicationManager instance] setTicker:[NSTimer scheduledTimerWithTimeInterval:1.0    target:self    ``selector:@selector(showActivity) userInfo:nil repeats:YES]];
}

-(IBAction) reset
{
[[[ApplicationManager instance] ticker] invalidate];
time.text = @" 0:00";
}

-(void) showActivity
{
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
time.text = [NSString stringWithFormat:@"%d", newTime];
}

*/


//
//  ApplicationManager.h
//  License
//
//  Created by Connor Gosell on 7/31/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//

#import <Foundation/Foundation.h>
@interface ApplicationManager : NSObject{

}

+(ApplicationManager*) instance;



@end
//
//  ApplicationManager.m
//  License
//
//  Created by Connor Gosell on 7/31/13.
//  Copyright (c) 2013 Connor Gosell. All rights reserved.
//
#import "ApplicationManager.h"
@implementation ApplicationManager
static ApplicationManager* appMgr = nil;

+(ApplicationManager*) instance
{
@synchronized([ApplicationManager class])
{
if(!appMgr)
{
appMgr = [[self alloc] init];
}

return appMgr;
}

return nil;
}

+(id) alloc
{
@synchronized([ApplicationManager class])
{
    NSAssert((appMgr == nil), @"Only one instance of singleton class may be      instantiated.");
appMgr = [super alloc];
return appMgr;
}
}

-(id) init
{
if(!(self = [super init]))
{
    [self release];
    return nil;
}

return self;
}
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
Gosell1
  • 37
  • 1
  • 9
  • In your `instance` class method, you never reach the end of the Synchronized block, the return must be outside the synchronized block. – lucaslt89 Aug 03 '13 at 04:55
  • 2
    It's not clear what code you're using now. You said you tried it in a singleton, but the code you posted shows the timer in a view controller. – rdelmar Aug 03 '13 at 04:57
  • @lucaslt89 can you explain it allittle more? I don't understand the synchronized block or which file? .m or .h – Gosell1 Aug 03 '13 at 04:57
  • @rdelmar I'm trying to use the singleton class, but to avoid errors, i put he code back to where it is now – Gosell1 Aug 03 '13 at 04:59
  • @Gosell1 look at the `ApplicationManager.m` file, in the `+(ApplicationManager*)` class method, the `return nil` instruction is never executed, and `return appMgr;` is executed inside the synchronized block. Only one synchronized block can be executed at time, but this block never ends – lucaslt89 Aug 03 '13 at 05:02
  • I don't think your singleton implementation is the recommended way to do that. Have a look at the answer here, http://stackoverflow.com/questions/8796529/singleton-in-ios-5 – rdelmar Aug 03 '13 at 05:57

2 Answers2

0

I'll make some assumptions, but you can calculate the time based on the system time instead of by incrementing a var. The start time can be stored as a property in the app delegate if it is reused by the entire app. If the time is calculated this way, timers can be started as each view controller appears and stopped when disappearing, and may benefit from a quicker interval if accuracy is desired. I use a similar technique to this in the Pace Clock Mac and iOS apps.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
0

Put the timer in your AppDelegate, have the timer tick send a notification, have your view controllers each enable a listener for the notification, so they can do their updates.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151