Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

Commit

Permalink
Quieter polling objects, updated timing semantics
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
numist committed Oct 17, 2013
1 parent 9143025 commit 6107640
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions NNKit/Actors/NNPollingObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#import "NNPollingObject.h"

#import "despatch.h"


@interface NNPollingObject ()

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 6107640

Please sign in to comment.