Created
February 9, 2013 02:34
-
-
Save veritech/4743573 to your computer and use it in GitHub Desktop.
Category to add Async methods to the FMDatabaseQueue
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
| // | |
| // 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