Skip to content

Instantly share code, notes, and snippets.

@JaviSoto
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaviSoto/8722367 to your computer and use it in GitHub Desktop.
Save JaviSoto/8722367 to your computer and use it in GitHub Desktop.
PBMainThreadSynchronousScheduler
#import <ReactiveCocoa/RACQueueScheduler+Subclass.h>
/**
* This scheduler class schedules values sent down a signal on the main thread synchronously
* if they're sent on the main thread as opposed to RACScheduler.mainThreadScheduler
* which always sends values asynchronously.
*/
@interface PBMainThreadSynchronousScheduler : RACQueueScheduler
+ (instancetype)mainThreadSynchronousScheduler;
@end
@implementation PBMainThreadSynchronousScheduler
+ (instancetype)mainThreadSynchronousScheduler {
static dispatch_once_t onceToken;
static PBMainThreadSynchronousScheduler *mainThreadSynchronousScheduler;
dispatch_once(&onceToken, ^{
mainThreadSynchronousScheduler = [[PBMainThreadSynchronousScheduler alloc] initWithName:@"com.getpebble.ios.RACScheduler.mainThreadSynchronousScheduler" queue:dispatch_get_main_queue()];
});
return mainThreadSynchronousScheduler;
}
- (RACDisposable *)schedule:(void (^)(void))block {
NSCParameterAssert(block != NULL);
RACDisposable *disposable = [[RACDisposable alloc] init];
dispatch_block_t performBlock = ^{
if (!disposable.disposed) {
[self performAsCurrentScheduler:block];
}
};
if ([NSThread isMainThread]) {
performBlock();
}
else {
dispatch_async(self.queue, performBlock);
}
return disposable;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment