Last active
August 29, 2015 13:55
-
-
Save JaviSoto/8722367 to your computer and use it in GitHub Desktop.
PBMainThreadSynchronousScheduler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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