Skip to content

Instantly share code, notes, and snippets.

@gtfunes
Created March 7, 2019 14:14
Show Gist options
  • Save gtfunes/3e713402e2590bc55849a9f1b94d6d6a to your computer and use it in GitHub Desktop.
Save gtfunes/3e713402e2590bc55849a9f1b94d6d6a to your computer and use it in GitHub Desktop.
An NSObject category (Obj-C) to perform code blocks in particular threads and with different priorities
//
// NSObject+performBlock.h
//
#import <Foundation/Foundation.h>
/**
An NSObject category to perform code blocks in particular threads and with different priorities
*/
@interface NSObject (PerformBlock)
- (void) performBlock:(void(^)(void))block;
- (void) performBlock:(void(^)(void))block afterDelay:(double)delayInSeconds;
- (void) performBlockInBackgroundQueue:(void(^)(void))block;
- (void) performBlockInBackgroundQueue:(void(^)(void))block afterDelay:(double)delayInSeconds;
- (void) performBlockOnMainQueue:(void(^)(void))block;
- (void) performBlockOnMainQueue:(void(^)(void))block afterDelay:(double)delayInSeconds;
- (void) performBlockOnMainQueueWithHighPriority:(void(^)(void))block;
- (void) performBlockOnMainQueueWithHighPriority:(void(^)(void))block afterDelay:(double)delayInSeconds;
@end
//
// NSObject+performBlock.m
//
#import "NSObject+performBlock.h"
@implementation NSObject (PerformBlock)
- (void) performBlock:(void(^)(void))block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
}
- (void) performBlock:(void(^)(void))block afterDelay:(double)delayInSeconds {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block);
}
- (void) performBlockInBackgroundQueue:(void(^)(void))block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);
}
- (void) performBlockInBackgroundQueue:(void(^)(void))block afterDelay:(double)delayInSeconds {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), block);
}
- (void) performBlockOnMainQueue:(void(^)(void))block {
dispatch_async(dispatch_get_main_queue(), block);
}
- (void) performBlockOnMainQueue:(void(^)(void))block afterDelay:(double)delayInSeconds {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), block);
}
- (void) performBlockOnMainQueueWithHighPriority:(void(^)(void))block {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), block);
}
- (void) performBlockOnMainQueueWithHighPriority:(void(^)(void))block afterDelay:(double)delayInSeconds {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), block);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment