Skip to content

Instantly share code, notes, and snippets.

@CavalcanteLeo
Forked from shyambhat/GCDManager.h
Created March 22, 2016 02:49
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 CavalcanteLeo/9f307c7aa5fb12d56401 to your computer and use it in GitHub Desktop.
Save CavalcanteLeo/9f307c7aa5fb12d56401 to your computer and use it in GitHub Desktop.
Clean Objective C helper methods to perform background tasks and perform completion on main thread.
#import <Foundation/Foundation.h>
@interface GCDManager : NSObject
+ (void)performBackgroundTask:(void (^)())block withCompletion:(void (^)())completion;
+ (void)performHighPriorityBackgroundTask:(void (^)())block withCompletion:(void (^)())completion;
+ (void)performLowPriorityBackgroundTask:(void (^)())block withCompletion:(void (^)())completion;
@end
#import "GCDManager.h"
@implementation GCDManager
+ (void)performBackgroundTask:(void (^)())block withCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
block();
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
});
}
+ (void)performHighPriorityBackgroundTask:(void (^)())block withCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
block();
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
});
}
+ (void)performLowPriorityBackgroundTask:(void (^)())block withCompletion:(void (^)())completion
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
block();
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment