Skip to content

Instantly share code, notes, and snippets.

@marcomasser
Forked from bsneed/NSObject+RFExtensions.h
Created October 11, 2012 17:44
Show Gist options
  • Save marcomasser/3874218 to your computer and use it in GitHub Desktop.
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.
//
// 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
//
// 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
//
// 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
//
// 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