Skip to content

Instantly share code, notes, and snippets.

@igavrysh
Last active September 11, 2017 15:13
Show Gist options
  • Save igavrysh/2fc48237970b23bcf8254464a617455e to your computer and use it in GitHub Desktop.
Save igavrysh/2fc48237970b23bcf8254464a617455e to your computer and use it in GitHub Desktop.
Adds Timer tracking funcitonality to NSObject
#import <objc/runtime.h>
#import "SMAGlobal.h"
#import "CocoaLumberjack.h"
#import "NSObject+SMATimer.h"
#import "SMAMacros.h"
#import "SMABlockMacros.h"
static void *smaStartTimeKey = "smaTimerKey";
@implementation NSObject (SMATimer)
#pragma mark -
#pragma mark Public Methods
- (void)startTimer {
objc_setAssociatedObject(self, smaStartTimeKey, [NSDate date], OBJC_ASSOCIATION_RETAIN);
}
- (void)stopTimerWithBlock:(void (^)(NSDate *startTime))block {
SMABlockPerform(block, objc_getAssociatedObject(self, smaStartTimeKey));
objc_setAssociatedObject(self, smaStartTimeKey, nil, OBJC_ASSOCIATION_RETAIN);
}
- (NSTimeInterval)stoppedTimeIntervalSinceStart {
__block NSTimeInterval interval = 0;
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[self stopTimerWithBlock:^(NSDate *start) {
interval = -[start timeIntervalSinceNow];
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
return interval;
}
- (void)stopLoadingInfoTimer {
SMALogInfo(@"%@ loading time interval %f sec",
[[self class] description],
[self stoppedTimeIntervalSinceStart]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment