Skip to content

Instantly share code, notes, and snippets.

@veritech
Created February 9, 2013 02:34
Show Gist options
  • Select an option

  • Save veritech/4743573 to your computer and use it in GitHub Desktop.

Select an option

Save veritech/4743573 to your computer and use it in GitHub Desktop.
Category to add Async methods to the FMDatabaseQueue
//
// FMDatabaseQueue+async.h
//
// Created by Jonathan Dalrymple on 05/02/2013.
//
#import <FMDB/FMDatabaseQueue.h>
#import <objc/runtime.h>
const static char *propertyName = "FR_asyncQueueProperty";
@interface FMDatabaseQueue (async)
@property (nonatomic,strong) dispatch_queue_t asyncQueue;
- (void)asyncInDatabase:(void (^)(FMDatabase *db))aBlock;
- (void)asyncInTransaction:(void (^)(FMDatabase *db, BOOL *rollback))aBlock;
@end
@implementation FMDatabaseQueue (async)
- (void)setAsyncQueue:(dispatch_queue_t)asyncQueue {
objc_setAssociatedObject(self, propertyName, asyncQueue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (dispatch_queue_t)asyncQueue {
dispatch_queue_t queue = NULL;
if (!(queue = objc_getAssociatedObject(self, propertyName))) {
queue = dispatch_queue_create("com.FMDatabaseQueue.async", DISPATCH_QUEUE_SERIAL);
[self setAsyncQueue:queue];
}
return queue;
}
- (void)asyncInDatabase:(void (^)(FMDatabase *db))aBlock {
__unsafe_unretained id weakSelf = self;
dispatch_async([self asyncQueue], ^{
[weakSelf inDatabase:aBlock];
});
}
- (void)asyncInTransaction:(void (^)(FMDatabase *db, BOOL *rollback))aBlock {
__unsafe_unretained id weakSelf = self;
dispatch_async([self asyncQueue], ^{
[weakSelf inTransaction:aBlock];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment