Permalink
Browse files

Merge pull request #11 from piemonte/master

queue love, updated basic types and declarations
  • Loading branch information...
2 parents 6b0ca73 + a7550d2 commit 5e68ee097515a94e5491eae6a49b55e49b935b83 @thisandagain committed Jan 28, 2014
Showing with 87 additions and 96 deletions.
  1. +25 −29 EDQueue/EDQueue.h
  2. +35 −35 EDQueue/EDQueue.m
  3. +2 −6 EDQueue/EDQueueStorageEngine.h
  4. +25 −26 EDQueue/EDQueueStorageEngine.m
View
@@ -8,48 +8,44 @@
#import <Foundation/Foundation.h>
-//
-
-typedef enum {
- EDQueueResultSuccess,
+typedef NS_ENUM(NSInteger, EDQueueResult) {
+ EDQueueResultSuccess = 0,
EDQueueResultFail,
EDQueueResultCritical
-} EDQueueResult;
+};
-UIKIT_EXTERN NSString *const EDQueueDidStart;
-UIKIT_EXTERN NSString *const EDQueueDidStop;
-UIKIT_EXTERN NSString *const EDQueueJobDidSucceed;
-UIKIT_EXTERN NSString *const EDQueueJobDidFail;
-UIKIT_EXTERN NSString *const EDQueueDidDrain;
+typedef void (^EDQueueCompletionBlock)(EDQueueResult result);
-//
+extern NSString *const EDQueueDidStart;
+extern NSString *const EDQueueDidStop;
+extern NSString *const EDQueueJobDidSucceed;
+extern NSString *const EDQueueJobDidFail;
+extern NSString *const EDQueueDidDrain;
-@class EDQueue;
-
-@protocol EDQueueDelegate <NSObject>
-@optional
-- (EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job;
-- (void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(void (^)(EDQueueResult result))block;
-@end
+@protocol EDQueueDelegate;
+@interface EDQueue : NSObject
-//
++ (EDQueue *)sharedInstance;
-@interface EDQueue : NSObject
+@property (nonatomic, weak) id<EDQueueDelegate> delegate;
-@property (weak) id<EDQueueDelegate> delegate;
-@property (readonly) Boolean isRunning;
-@property (readonly) Boolean isActive;
-@property NSUInteger retryLimit;
+@property (nonatomic, readonly) BOOL isRunning;
+@property (nonatomic, readonly) BOOL isActive;
+@property (nonatomic) NSUInteger retryLimit;
-+ (EDQueue *)sharedInstance;
- (void)enqueueWithData:(id)data forTask:(NSString *)task;
-
- (void)start;
- (void)stop;
- (void)empty;
-- (Boolean)jobExistsForTask:(NSString *)task;
-- (Boolean)jobIsActiveForTask:(NSString *)task;
+- (BOOL)jobExistsForTask:(NSString *)task;
+- (BOOL)jobIsActiveForTask:(NSString *)task;
- (NSDictionary *)nextJobForTask:(NSString *)task;
-@end
+@end
+
+@protocol EDQueueDelegate <NSObject>
+@optional
+- (EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job;
+- (void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(EDQueueCompletionBlock)block;
+@end
View
@@ -9,54 +9,62 @@
#import "EDQueue.h"
#import "EDQueueStorageEngine.h"
-//
-
-#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
-static dispatch_once_t pred = 0; \
-__strong static id _sharedObject = nil; \
-dispatch_once(&pred, ^{ \
-_sharedObject = block(); \
-}); \
-return _sharedObject; \
-
NSString *const EDQueueDidStart = @"EDQueueDidStart";
NSString *const EDQueueDidStop = @"EDQueueDidStop";
NSString *const EDQueueJobDidSucceed = @"EDQueueJobDidSucceed";
NSString *const EDQueueJobDidFail = @"EDQueueJobDidFail";
NSString *const EDQueueDidDrain = @"EDQueueDidDrain";
@interface EDQueue ()
-@property EDQueueStorageEngine *engine;
-@property (readwrite) Boolean isRunning;
-@property (readwrite) Boolean isActive;
-@property (readwrite) NSString *activeTask;
+{
+ BOOL _isRunning;
+ BOOL _isActive;
+ NSUInteger _retryLimit;
+}
+
+@property (nonatomic) EDQueueStorageEngine *engine;
+@property (nonatomic, readwrite) NSString *activeTask;
+
@end
//
@implementation EDQueue
-#pragma mark - Init
+@synthesize isRunning = _isRunning;
+@synthesize isActive = _isActive;
+@synthesize retryLimit = _retryLimit;
+
+#pragma mark - Singleton
+ (EDQueue *)sharedInstance
{
- DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
- return [[self alloc] init];
+ static EDQueue *singleton = nil;
+ static dispatch_once_t once = 0;
+ dispatch_once(&once, ^{
+ singleton = [[self alloc] init];
});
+ return singleton;
}
+#pragma mark - Init
+
- (id)init
{
self = [super init];
if (self) {
_engine = [[EDQueueStorageEngine alloc] init];
- _isRunning = false;
- _isActive = false;
_retryLimit = 4;
}
return self;
}
+- (void)dealloc
+{
+ self.delegate = nil;
+ _engine = nil;
+}
+
#pragma mark - Public methods
/**
@@ -81,9 +89,9 @@ - (void)enqueueWithData:(id)data forTask:(NSString *)task
*
* @return {Boolean}
*/
-- (Boolean)jobExistsForTask:(NSString *)task
+- (BOOL)jobExistsForTask:(NSString *)task
{
- Boolean jobExists = [self.engine jobExistsForTask:task];
+ BOOL jobExists = [self.engine jobExistsForTask:task];
return jobExists;
}
@@ -94,9 +102,9 @@ - (Boolean)jobExistsForTask:(NSString *)task
*
* @return {Boolean}
*/
-- (Boolean)jobIsActiveForTask:(NSString *)task
+- (BOOL)jobIsActiveForTask:(NSString *)task
{
- Boolean jobIsActive = [self.activeTask length] > 0 && [self.activeTask isEqualToString:task];
+ BOOL jobIsActive = [self.activeTask length] > 0 && [self.activeTask isEqualToString:task];
return jobIsActive;
}
@@ -121,7 +129,7 @@ - (NSDictionary *)nextJobForTask:(NSString *)task
- (void)start
{
if (!self.isRunning) {
- self.isRunning = true;
+ _isRunning = YES;
[self tick];
[self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueDidStart, @"name", nil, @"data", nil] waitUntilDone:false];
}
@@ -136,7 +144,7 @@ - (void)start
- (void)stop
{
if (self.isRunning) {
- self.isRunning = false;
+ _isRunning = YES;
[self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueDidStop, @"name", nil, @"data", nil] waitUntilDone:false];
}
}
@@ -168,7 +176,7 @@ - (void)tick
dispatch_async(gcd, ^{
if (self.isRunning && !self.isActive && [self.engine fetchJobCount] > 0) {
// Start job
- self.isActive = true;
+ _isActive = YES;
id job = [self.engine fetchJob];
self.activeTask = [(NSDictionary *)job objectForKey:@"task"];
@@ -212,7 +220,7 @@ - (void)processJob:(NSDictionary*)job withResult:(EDQueueResult)result
}
// Clean-up
- self.isActive = false;
+ _isActive = NO;
// Drain
if ([self.engine fetchJobCount] == 0) {
@@ -248,12 +256,4 @@ - (void)errorWithMessage:(NSString *)message
NSLog(@"EDQueue Error: %@", message);
}
-#pragma mark - Dealloc
-
-- (void)dealloc
-{
- self.delegate = nil;
- _engine = nil;
-}
-
@end
@@ -8,17 +8,13 @@
#import <Foundation/Foundation.h>
-#import "FMDatabase.h"
-#import "FMDatabaseAdditions.h"
-#import "FMDatabasePool.h"
-#import "FMDatabaseQueue.h"
-
+@class FMDatabaseQueue;
@interface EDQueueStorageEngine : NSObject
@property (retain) FMDatabaseQueue *queue;
- (void)createJob:(id)data forTask:(id)task;
-- (Boolean)jobExistsForTask:(id)task;
+- (BOOL)jobExistsForTask:(id)task;
- (void)incrementAttemptForJob:(NSNumber *)jid;
- (void)removeJob:(NSNumber *)jid;
- (void)removeAllJobs;
Oops, something went wrong.

0 comments on commit 5e68ee0

Please sign in to comment.