From 6107640b7d051bb34431dcf01030815537937266 Mon Sep 17 00:00:00 2001 From: Scott Perry Date: Wed, 16 Oct 2013 22:57:39 -0700 Subject: [PATCH] Quieter polling objects, updated timing semantics The NNPollingObject superclass no longer logs anything, it is up to each subclass to perform any timing or liveness checks that they may need. The poll timing has also changed. No longer is the time spent performing the last iteration credited to the next interval, each iteration begins a full interval after the previous completed. In addition, notification dispatch blocks execution of the caller, yielding the worker queue to other jobs on the runloop until the main thread has finished processing the notification. --- NNKit/Actors/NNPollingObject.m | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/NNKit/Actors/NNPollingObject.m b/NNKit/Actors/NNPollingObject.m index 7e3ebc4..25dc13f 100644 --- a/NNKit/Actors/NNPollingObject.m +++ b/NNKit/Actors/NNPollingObject.m @@ -14,6 +14,8 @@ #import "NNPollingObject.h" +#import "despatch.h" + @interface NNPollingObject () @@ -50,22 +52,15 @@ - (instancetype)init; - (void)workerLoop; { - NSDate *start = [NSDate date]; - [self main]; - if (self.interval <= 0.0) { - NSLog(@"Interval is negative, disabling polling loop for %@", self); + NSTimeInterval interval = self.interval; + if (interval <= 0.0) { return; } - double elapsed = [[NSDate date] timeIntervalSinceDate:start]; - if (elapsed > self.interval) { - NSLog(@"Worker loop interation took %f seconds (interval %f)!", elapsed, self.interval); - } - __weak id weakSelf = self; - double delayInSeconds = MAX(self.interval - elapsed, 0.0); + double delayInSeconds = interval; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, self.queue, ^(void){ id self = weakSelf; @@ -75,9 +70,13 @@ - (void)workerLoop; - (void)postNotification:(NSDictionary *)userInfo; { + dispatch_group_t group = dispatch_group_create(); + dispatch_group_enter(group); dispatch_async(dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:[[self class] notificationName] object:self userInfo:userInfo]; + dispatch_group_leave(group); }); + while(!despatch_group_yield(group)); } - (void)main;