Skip to content

Instantly share code, notes, and snippets.

@bsneed
Created October 6, 2012 06:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bsneed/3844232 to your computer and use it in GitHub Desktop.
Save bsneed/3844232 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.
// Copyright (c) 2012 redf.net. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^NSObjectPerformBlock)(id userObject);
@interface NSObject (RFExtensions)
- (void)performBlockInBackground:(NSObjectPerformBlock)performBlock completion:(NSObjectPerformBlock)completionBlock userObject:(id)userObject;
@end
//
// NSObject+RFExtensions.m
//
// Created by brandon on 10/5/12.
// Copyright (c) 2012 redf.net. All rights reserved.
//
#import "NSObject+RFExtensions.h"
@implementation NSObject (RFExtensions)
- (void)performBlockInBackground:(NSObjectPerformBlock)performBlock completion:(NSObjectPerformBlock)completionBlock userObject:(id)userObject
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
performBlock(userObject);
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(userObject);
});
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment