0

application design/architecture question for a project that I am building.

I have a main controller which holds an NSMutableArray of currently "active" objects. Each one of those objects, when instantiated, will have their own NSTimer that is timing the amount of seconds / minutes / hours that the object is in an "active" state.

Should I be generating and allocating these NSTimers on their own separate thread when a user creates the new object? Or can I have multiple timers like this running on the main thread?

Not that familiar with threading so that is why I'm asking. If each object has it's own NSTimer allocated then threading isn't necessary? or...?

skålfyfan
  • 4,931
  • 5
  • 41
  • 59
  • You should not create new threads if not absolutely necessary as it requires a lot of work for the OS. Apple recommends to use Grand Central Dispatch for multithreading purposes. Although here you don't actually need timers at all ( as the guys below already answered ) – guitarflow Apr 16 '12 at 19:04

2 Answers2

1

I don't think you need NSTimer for this. NSTimer is used to fire a callback after a set amount of time. If you are trying to track the amount of time since an object has been created, do this:

Upon making object active:

object.createdAt = [ NSDate date ] ;

To determine how long the object has been active, do this:

NSTimeInterval numberOfSecondsSinceCreation = [ [ NSDate date ] timeIntervalSinceDate:object.createdAt ] ;

pausable implementation

I wrote a category you can add to your project that you can use to track 'active time' for objects.

@interface NSObject (Timeable)
@property ( nonatomic ) NSTimeInterval totalActiveTime ;
@property ( nonatomic, retain ) NSDate * lastStartTime ;
@end

@implementation NSObject (Timeable)

-(void)startTimer
{
    self.lastStartTime = [ NSDate date ] ;
}

-(void)pauseTimer
{
    self.totalActiveTime = self.totalActiveTime + [[ NSDate date ] timeIntervalSinceDate:self.lastStartTime ] ;
}

-(NSDate*)lastStartTime
{
    return objc_getAssociatedObject( self, @"lastStartTime" ) ;
}

-(void)setLastStartTime:(NSDate*)date
{
    return objc_setAssociatedObject( self, @"lastStartTime", date, OBJC_ASSOCIATION_RETAIN_NONATOMIC ) ;
}

-(NSTimeInterval)totalActiveTime
{
    NSNumber * number = objc_getAssociatedObject( self, @"totalActiveTime" ) ;
    return  number ? [ number doubleValue ] : 0.0 ;
}

-(void)setTotalActiveTime:(NSTimeInterval)time
{
    objc_setAssociatedObject( self, @"totalActiveTime", [ NSNumber numberWithDouble:time ], OBJC_ASSOCIATION_RETAIN_NONATOMIC ) ;
}

@end
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • THe user needs to be able to PAUSE the timer though so that's why I believe NSTimer is necessary? Yes no? – skålfyfan Apr 16 '12 at 19:09
  • @Skal: You can't pause an `NSTimer` anyways. Just keep adding to the "time active" variable whenever the object switches from active to inactive. – jscs Apr 16 '12 at 19:11
  • Ah okay. I think I get it now. But if I wanted to also DISPLAY and render the time elapsed every second into an NSMenuItem - then I would need an NSTimer event which fires every second to update right? I think I got it now. Thanks. – skålfyfan Apr 16 '12 at 19:15
  • Yes, you can use an NSTimer to periodically update your menu item. I added some code you can use to add the functionality you want to any object. – nielsbot Apr 16 '12 at 19:21
0

You can have many timers on the main thread if you need to, but from your description, you don't need to. NSTimers are for doing something after a period of time, optionally repeated; they're not for measuring duration.

If you need to know the time interval that your object is "active", just record the date when it becomes active ([NSDate date]), record the date when it becomes inactive, and subtract ([[NSDate date] timeIntervalSinceDate:activationDate];).

- (void) beginRockingAndRolling {

    self.lastActivationDate = [NSDate date];

    // Rock and roll!

}

- (void) stopRockingAndRolling {

    self.totalTimeActive += [[NSDate date] timeIntervalSinceDate:self.lastActivationDate];

    // Turn off the rock

}
jscs
  • 63,694
  • 13
  • 151
  • 195