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