-
-
Save marcomasser/3874218 to your computer and use it in GitHub Desktop.
perform a block in a background thread, and call a completion block on the main thread when its done.
This file contains hidden or 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
// | |
// NSObject+RFExtensions.h | |
// | |
// Created by brandon on 10/5/12. | |
// Modified by Marco Masser on 2012-10-11 | |
// Copyright (c) 2012 redf.net. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
typedef void (^RFObjectPerformBlock)(void); | |
@interface NSObject (RFExtensions) | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionBlockOnMainQueue:(RFObjectPerformBlock)completionBlock; | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock; | |
- (void)performOnQueue:(dispatch_queue_t)performQueue block:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock; | |
@end |
This file contains hidden or 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
// | |
// NSObject+RFExtensions.m | |
// | |
// Created by brandon on 10/5/12. | |
// Modified by Marco Masser on 2012-10-11 | |
// Copyright (c) 2012 redf.net. All rights reserved. | |
// | |
#import "NSObject+RFExtensions.h" | |
@implementation NSObject (RFExtensions) | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionBlockOnMainQueue:(RFObjectPerformBlock)completionBlock | |
{ | |
[self performOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
block:performBlock | |
completionQueue:dispatch_get_main_queue() | |
completionBlock:completionBlock]; | |
} | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock | |
{ | |
[self performOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
block:performBlock | |
completionQueue:completionQueue | |
completionBlock:completionBlock]; | |
} | |
- (void)performOnQueue:(dispatch_queue_t)performQueue block:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock | |
{ | |
dispatch_async(performQueue, ^{ | |
performBlock(); | |
dispatch_async(completionQueue, ^{ | |
completionBlock(); | |
}); | |
}); | |
} | |
@end |
This file contains hidden or 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
// | |
// NSObject+RFExtensions.h | |
// | |
// Created by brandon on 10/5/12. | |
// Modified by Marco Masser on 2012-10-11 | |
// Copyright (c) 2012 redf.net. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
typedef void (^RFObjectPerformBlock)(void); | |
@interface NSObject (RFExtensions) | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionBlockOnMainQueue:(RFObjectPerformBlock)completionBlock; | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock; | |
- (void)performOnQueue:(dispatch_queue_t)performQueue block:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock; | |
@end |
This file contains hidden or 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
// | |
// NSObject+RFExtensions.m | |
// | |
// Created by brandon on 10/5/12. | |
// Modified by Marco Masser on 2012-10-11 | |
// Copyright (c) 2012 redf.net. All rights reserved. | |
// | |
#import "NSObject+RFExtensions.h" | |
@implementation NSObject (RFExtensions) | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionBlockOnMainQueue:(RFObjectPerformBlock)completionBlock | |
{ | |
[self performOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
block:performBlock | |
completionQueue:dispatch_get_main_queue() | |
completionBlock:completionBlock]; | |
} | |
- (void)performBlockInBackground:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock | |
{ | |
[self performOnQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
block:performBlock | |
completionQueue:completionQueue | |
completionBlock:completionBlock]; | |
} | |
- (void)performOnQueue:(dispatch_queue_t)performQueue block:(RFObjectPerformBlock)performBlock completionQueue:(dispatch_queue_t)completionQueue completionBlock:(RFObjectPerformBlock)completionBlock | |
{ | |
[self retain]; | |
dispatch_async(performQueue, ^{ | |
performBlock(); | |
dispatch_async(completionQueue, ^{ | |
completionBlock(); | |
[self autorelease]; | |
}); | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment