Skip to content

Instantly share code, notes, and snippets.

@trojanfoe
Created February 20, 2014 11:50
Show Gist options
  • Save trojanfoe/9111894 to your computer and use it in GitHub Desktop.
Save trojanfoe/9111894 to your computer and use it in GitHub Desktop.
Sleeping for a set period of time while running the runloop
$ clang -DDEBUG=1 -g -fobjc-arc -o rlsleep rlsleep.m -framework Foundation
$ ./rlsleep
2014-02-20 11:45:36.481 rlsleep[95410:707] Before 2014-02-20 11:45:36 +0000
2014-02-20 11:45:37.475 rlsleep[95410:707] Tick
2014-02-20 11:45:38.476 rlsleep[95410:707] Tick
2014-02-20 11:45:39.475 rlsleep[95410:707] Tick
2014-02-20 11:45:40.475 rlsleep[95410:707] Tick
2014-02-20 11:45:41.475 rlsleep[95410:707] Tick
2014-02-20 11:45:42.476 rlsleep[95410:707] Tick
2014-02-20 11:45:43.476 rlsleep[95410:707] Tick
2014-02-20 11:45:44.476 rlsleep[95410:707] Tick
2014-02-20 11:45:45.475 rlsleep[95410:707] Tick
2014-02-20 11:45:46.476 rlsleep[95410:707] Tick
2014-02-20 11:45:47.476 rlsleep[95410:707] Tick
2014-02-20 11:45:48.475 rlsleep[95410:707] Tick
2014-02-20 11:45:49.476 rlsleep[95410:707] Tick
2014-02-20 11:45:50.475 rlsleep[95410:707] Tick
2014-02-20 11:45:51.475 rlsleep[95410:707] Tick
2014-02-20 11:45:52.476 rlsleep[95410:707] Tick
2014-02-20 11:45:53.476 rlsleep[95410:707] Tick
2014-02-20 11:45:54.476 rlsleep[95410:707] Tick
2014-02-20 11:45:55.475 rlsleep[95410:707] Tick
2014-02-20 11:45:56.475 rlsleep[95410:707] Tick
2014-02-20 11:45:57.476 rlsleep[95410:707] Tick
2014-02-20 11:45:58.476 rlsleep[95410:707] Tick
2014-02-20 11:45:59.475 rlsleep[95410:707] Tick
2014-02-20 11:46:00.476 rlsleep[95410:707] Tick
2014-02-20 11:46:01.476 rlsleep[95410:707] Tick
2014-02-20 11:46:02.475 rlsleep[95410:707] Tick
2014-02-20 11:46:03.475 rlsleep[95410:707] Tick
2014-02-20 11:46:04.476 rlsleep[95410:707] Tick
2014-02-20 11:46:05.476 rlsleep[95410:707] Tick
2014-02-20 11:46:06.476 rlsleep[95410:707] Tick
2014-02-20 11:46:07.475 rlsleep[95410:707] Tick
2014-02-20 11:46:08.476 rlsleep[95410:707] Tick
2014-02-20 11:46:09.475 rlsleep[95410:707] Tick
2014-02-20 11:46:10.476 rlsleep[95410:707] Tick
2014-02-20 11:46:11.476 rlsleep[95410:707] Tick
2014-02-20 11:46:12.476 rlsleep[95410:707] Tick
2014-02-20 11:46:13.476 rlsleep[95410:707] Tick
2014-02-20 11:46:14.476 rlsleep[95410:707] Tick
2014-02-20 11:46:15.476 rlsleep[95410:707] Tick
2014-02-20 11:46:16.476 rlsleep[95410:707] Tick
2014-02-20 11:46:17.476 rlsleep[95410:707] Tick
2014-02-20 11:46:18.476 rlsleep[95410:707] Tick
2014-02-20 11:46:19.476 rlsleep[95410:707] Tick
2014-02-20 11:46:20.476 rlsleep[95410:707] Tick
2014-02-20 11:46:21.476 rlsleep[95410:707] Tick
2014-02-20 11:46:22.476 rlsleep[95410:707] Tick
2014-02-20 11:46:23.476 rlsleep[95410:707] Tick
2014-02-20 11:46:24.476 rlsleep[95410:707] Tick
2014-02-20 11:46:25.476 rlsleep[95410:707] Tick
2014-02-20 11:46:26.476 rlsleep[95410:707] Tick
2014-02-20 11:46:27.477 rlsleep[95410:707] Tick
2014-02-20 11:46:28.477 rlsleep[95410:707] Tick
2014-02-20 11:46:29.476 rlsleep[95410:707] Tick
2014-02-20 11:46:30.476 rlsleep[95410:707] Tick
2014-02-20 11:46:31.477 rlsleep[95410:707] Tick
2014-02-20 11:46:32.477 rlsleep[95410:707] Tick
2014-02-20 11:46:33.477 rlsleep[95410:707] Tick
2014-02-20 11:46:34.476 rlsleep[95410:707] Tick
2014-02-20 11:46:35.477 rlsleep[95410:707] Tick
2014-02-20 11:46:36.476 rlsleep[95410:707] Tick
2014-02-20 11:46:37.476 rlsleep[95410:707] Tick
2014-02-20 11:46:38.477 rlsleep[95410:707] Tick
2014-02-20 11:46:39.476 rlsleep[95410:707] Tick
2014-02-20 11:46:40.477 rlsleep[95410:707] Tick
2014-02-20 11:46:41.477 rlsleep[95410:707] Tick
2014-02-20 11:46:42.477 rlsleep[95410:707] Tick
2014-02-20 11:46:43.477 rlsleep[95410:707] Tick
2014-02-20 11:46:44.476 rlsleep[95410:707] Tick
2014-02-20 11:46:45.476 rlsleep[95410:707] Tick
2014-02-20 11:46:46.477 rlsleep[95410:707] Tick
2014-02-20 11:46:47.477 rlsleep[95410:707] Tick
2014-02-20 11:46:48.476 rlsleep[95410:707] Tick
2014-02-20 11:46:49.476 rlsleep[95410:707] Tick
2014-02-20 11:46:50.477 rlsleep[95410:707] Tick
2014-02-20 11:46:51.476 rlsleep[95410:707] Tick
2014-02-20 11:46:52.476 rlsleep[95410:707] Tick
2014-02-20 11:46:53.476 rlsleep[95410:707] Tick
2014-02-20 11:46:54.477 rlsleep[95410:707] Tick
2014-02-20 11:46:55.477 rlsleep[95410:707] Tick
2014-02-20 11:46:56.477 rlsleep[95410:707] Tick
2014-02-20 11:46:57.476 rlsleep[95410:707] Tick
2014-02-20 11:46:58.476 rlsleep[95410:707] Tick
2014-02-20 11:46:59.477 rlsleep[95410:707] Tick
2014-02-20 11:47:00.477 rlsleep[95410:707] Tick
2014-02-20 11:47:01.477 rlsleep[95410:707] Tick
2014-02-20 11:47:02.477 rlsleep[95410:707] Tick
2014-02-20 11:47:03.477 rlsleep[95410:707] Tick
2014-02-20 11:47:04.477 rlsleep[95410:707] Tick
2014-02-20 11:47:05.476 rlsleep[95410:707] Tick
2014-02-20 11:47:06.476 rlsleep[95410:707] Tick
2014-02-20 11:47:07.476 rlsleep[95410:707] Tick
2014-02-20 11:47:08.477 rlsleep[95410:707] Tick
2014-02-20 11:47:09.477 rlsleep[95410:707] Tick
2014-02-20 11:47:10.477 rlsleep[95410:707] Tick
2014-02-20 11:47:11.477 rlsleep[95410:707] Tick
2014-02-20 11:47:12.477 rlsleep[95410:707] Tick
2014-02-20 11:47:13.477 rlsleep[95410:707] Tick
2014-02-20 11:47:14.477 rlsleep[95410:707] Tick
2014-02-20 11:47:15.477 rlsleep[95410:707] Tick
2014-02-20 11:47:16.476 rlsleep[95410:707] Tick
2014-02-20 11:47:17.477 rlsleep[95410:707] Tick
2014-02-20 11:47:18.477 rlsleep[95410:707] Tick
2014-02-20 11:47:19.477 rlsleep[95410:707] Tick
2014-02-20 11:47:20.477 rlsleep[95410:707] Tick
2014-02-20 11:47:21.477 rlsleep[95410:707] Tick
2014-02-20 11:47:22.477 rlsleep[95410:707] Tick
2014-02-20 11:47:23.477 rlsleep[95410:707] Tick
2014-02-20 11:47:24.477 rlsleep[95410:707] Tick
2014-02-20 11:47:25.478 rlsleep[95410:707] Tick
2014-02-20 11:47:26.477 rlsleep[95410:707] Tick
2014-02-20 11:47:27.478 rlsleep[95410:707] Tick
2014-02-20 11:47:28.478 rlsleep[95410:707] Tick
2014-02-20 11:47:29.477 rlsleep[95410:707] Tick
2014-02-20 11:47:30.478 rlsleep[95410:707] Tick
2014-02-20 11:47:31.477 rlsleep[95410:707] Tick
2014-02-20 11:47:32.477 rlsleep[95410:707] Tick
2014-02-20 11:47:33.477 rlsleep[95410:707] Tick
2014-02-20 11:47:34.477 rlsleep[95410:707] Tick
2014-02-20 11:47:35.477 rlsleep[95410:707] Tick
2014-02-20 11:47:36.478 rlsleep[95410:707] Tick
2014-02-20 11:47:36.486 rlsleep[95410:707] After 2014-02-20 11:47:36 +0000
#import <Foundation/Foundation.h>
@interface SomeClass : NSObject
+ (void)runRunLoopForTimeInterval:(NSTimeInterval)timeInterval;
@end
@implementation SomeClass
+ (void)runRunLoopForTimeInterval:(NSTimeInterval)timeInterval {
NSDate *stopTime = [NSDate dateWithTimeIntervalSinceNow:timeInterval];
while ([stopTime compare:[NSDate date]] == NSOrderedDescending) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:stopTime];
}
}
@end
@interface TimerTarget : NSObject
- (void)tick:(NSTimer *)timer;
@end
@implementation TimerTarget
- (void)tick:(NSTimer *)timer {
NSLog(@"Tick");
}
@end
int main(int argc, const char **argv) {
@autoreleasepool {
TimerTarget *target = [TimerTarget new];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:target
selector:@selector(tick:)
userInfo:nil
repeats:YES];
NSLog(@"Before %@", [NSDate date]);
[SomeClass runRunLoopForTimeInterval:2 * 60];
NSLog(@"After %@", [NSDate date]);
[timer invalidate];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment