Skip to content

Instantly share code, notes, and snippets.

@jonnolen
Created August 6, 2013 14:48
Show Gist options
  • Save jonnolen/6165157 to your computer and use it in GitHub Desktop.
Save jonnolen/6165157 to your computer and use it in GitHub Desktop.
// An object that will run a piece of code every <<interval>> seconds while the application has connectivity to the network,
// automatically pauses the loop when isNetworkReachable is false.
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <ReactiveCocoa/NSNotificationCenter+RACSupport.h>
@interface PWOfflineUpdater : NSObject
-(id)initWithUpdateInterval:(NSTimeInterval)interval targetOperationQueue:(NSOperationQueue *)queue;
-(void)start;
-(void)stop;
+(NSNotificationCenter *)notificationCenter;
@end
@implementation PWOfflineUpdater{
NSOperationQueue *__weak queue;
NSTimeInterval update_interval;
RACDisposable *workSubscription;
}
-(id)initWithUpdateInterval:(NSTimeInterval)interval targetOperationQueue:(NSOperationQueue *)targetQueue{
if (self = [super init]){
update_interval = interval;
queue = targetQueue;
}
return self;
}
//subscribe to the do work signal.
-(void)start{
workSubscription = [self.doWorkSignal subscribeNext:^(NSDate *date){
NSLog(@"BACON -> should be fetching some things.");
}];
}
//unsubscribe to the do work signal.
-(void)stop{
[workSubscription dispose];
workSubscription = nil;
}
-(void)dealloc{
[self stop];
}
//Signal that immediately returns the current reachability state.
-(RACSignal *)currentReachabilitySignal{
return [RACSignal return:@(self.class.reachabilityHandler.isNetworkReachable)];
}
//signal that maps reachability notifications into true/false values and only sendsNext on a change.
-(RACSignal *)booleanReachabilityNotificationSignal{
return [[[self.class.notificationCenter rac_addObserverForName:PWReachabilityChangedNotification object:nil]
map:^NSNumber *(NSNotification *notification){
PWReachabilityHandler *handler = (PWReachabilityHandler *)notification.object;
return @(handler.isNetworkReachable);
}]
distinctUntilChanged];
}
//signal that concatenates the notification signal onto the end of the current reachability signal.
-(RACSignal *)reachabilitySignalWithStart{
return [[self currentReachabilitySignal] concat:[self booleanReachabilityNotificationSignal]];
}
// signal that posts a date every <<interval>> seconds while the network reachability signal has
// a value of true and sends nothing while the reachability signal has a value of false.
-(RACSignal *)doWorkSignal{
RACSignal *reachable = [self reachabilitySignalWithStart];
RACSignal *update = [RACSignal interval:update_interval];
RACSignal *nothing = [RACSignal empty];
return [RACSignal if:reachable then:update else:nothing];
}
//Testing Swizzle Points.
+(NSNotificationCenter *)notificationCenter{
return [NSNotificationCenter defaultCenter];
}
+(PWReachabilityHandler *)reachabilityHandler{
return [PWReachabilityHandler sharedHandler];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment