Skip to content

Instantly share code, notes, and snippets.

@robpearson
Created February 17, 2015 00:37
Show Gist options
  • Save robpearson/3c8e0d7f536ff57cf5ea to your computer and use it in GitHub Desktop.
Save robpearson/3c8e0d7f536ff57cf5ea to your computer and use it in GitHub Desktop.
Everyday Transit Hourly Interval Signal with an initial value.
#import "DateTools.h"
#import "RACEXTScope.h"
#import "ReactiveCocoa.h"
@interface MPXTransitDashboardViewModel ()
/// ...
@end
@implementation MPXTransitDashboardViewModel
- (instancetype)init {
// ...
[self bindSignals];
}
- (void)bindSignals {
// ...
RACSignal hourlyIntervalRefreshSignal = [self hourlyIntervalRefreshSignal];
// ...
}
- (RACSignal *)hourlyIntervalRefreshSignal {
NSDate *now = [NSDate date];
NSString *initialEventDateString = [NSString stringWithFormat:@"%d-%d-%d %d:00:00", now.year, now.month, now.day, now.hour + 1];
[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *initialEventDate = [self.dateFormatter dateFromString:initialEventDateString];
NSTimeInterval initialEventDelay = [now secondsEarlierThan:initialEventDate];
DDLogDebug(@"%@ - Hourly Interval Signal. Initial Event Date: %@ Seconds: %f", NSStringFromClass([self class]), initialEventDateString, initialEventDelay);
// Signal sends an initial value
RACSignal *bootstrapSignal = [[RACSignal return:[NSDate date]] setNameWithFormat:@"Hourly Boostrap Event Signal"];
// Then sends another event when the first hour rolls over
// DAVE: Initially, I hadd a [RACSignal Empty] -delay:xx] but it never worked. I found it needed an initial event sent through before completion for the delay to take effect.
RACSignal *initialEventSignal = [[[RACSignal return:[NSDate date]] delay:initialEventDelay] setNameWithFormat:@"Hourly Initial Event Signal"];
// Then rolls over every hour thereafter ...
RACSignal *intervalEventSignal = [[RACSignal
interval:60 * 60 // Refresh Every Hour
onScheduler:[RACScheduler schedulerWithPriority:RACSchedulerPriorityDefault]]
setNameWithFormat:@"Transit Trips Interval Refresh Signal"];
RACSignal *bootstrappedInitialEventSignal = [bootstrapSignal concat:initialEventSignal];
RACSignal *intervalRefreshSignal = [bootstrappedInitialEventSignal concat:intervalEventSignal];
return intervalRefreshSignal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment