Skip to content

Commit 9adb89e

Browse files
committed
Introduced Persistent Storage Injection for Queue
Updated Code & Example Added basic Queue tests README updated accordingly
1 parent 9d08d34 commit 9adb89e

14 files changed

+475
-138
lines changed

EDQueue.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Pod::Spec.new do |s|
22
s.name = 'EDQueue'
3-
s.version = '0.7.2'
3+
s.version = '0.7.3'
44
s.license = 'MIT'
55
s.summary = 'A persistent background job queue for iOS.'
66
s.homepage = 'https://github.com/gelosi/queue'
77
s.authors = {'Andrew Sliwinski' => '[email protected]', 'Francois Lambert' => '[email protected]', 'Oleg Shanyuk' => '[email protected]'}
8-
s.source = { :git => 'https://github.com/gelosi/queue.git', :tag => 'v0.7.2' }
8+
s.source = { :git => 'https://github.com/gelosi/queue.git', :tag => 'v0.7.3' }
99
s.platform = :ios, '7.0'
1010
s.source_files = 'EDQueue'
1111
s.library = 'sqlite3.0'

EDQueue/EDQueue.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
// Copyright (c) 2012 Andrew Sliwinski. All rights reserved.
77
//
88

9-
#import <Foundation/Foundation.h>
9+
@import Foundation;
1010

1111
#import "EDQueueJob.h"
12+
#import "EDQueuePersistentStorageProtocol.h"
1213

1314
NS_ASSUME_NONNULL_BEGIN
1415

@@ -26,22 +27,32 @@ extern NSString *const EDQueueJobDidSucceed;
2627
extern NSString *const EDQueueJobDidFail;
2728
extern NSString *const EDQueueDidDrain;
2829

29-
extern NSString *const EDQueueNameKey;
30-
extern NSString *const EDQueueDataKey;
30+
@class EDQueue;
3131

32-
33-
@protocol EDQueueDelegate;
32+
@protocol EDQueueDelegate <NSObject>
33+
- (void)queue:(EDQueue *)queue processJob:(EDQueueJob *)job completion:(EDQueueCompletionBlock)block;
34+
@end
3435

3536
@interface EDQueue : NSObject
3637

37-
+ (EDQueue *)sharedInstance;
38-
3938
@property (nonatomic, weak) id<EDQueueDelegate> delegate;
39+
@property (nonatomic, strong, readonly) id<EDQueuePersistentStorage> storage;
4040

41+
/**
42+
* Returns true if Queue is running (e.g. not stopped).
43+
*/
4144
@property (nonatomic, readonly) BOOL isRunning;
45+
/**
46+
* Returns true if Queue is performing Job right now
47+
*/
4248
@property (nonatomic, readonly) BOOL isActive;
49+
/**
50+
* Retry limit for failing tasks (will be elimitated and moved to Job later)
51+
*/
4352
@property (nonatomic) NSUInteger retryLimit;
4453

54+
- (instancetype)initWithPersistentStore:(id<EDQueuePersistentStorage>)persistentStore;
55+
4556
- (void)enqueueJob:(EDQueueJob *)job;
4657
- (void)start;
4758
- (void)stop;
@@ -53,8 +64,5 @@ extern NSString *const EDQueueDataKey;
5364

5465
@end
5566

56-
@protocol EDQueueDelegate <NSObject>
57-
- (void)queue:(EDQueue *)queue processJob:(EDQueueJob *)job completion:(EDQueueCompletionBlock)block;
58-
@end
5967

6068
NS_ASSUME_NONNULL_END

EDQueue/EDQueue.m

+15-55
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,31 @@
1515
NSString *const EDQueueJobDidFail = @"EDQueueJobDidFail";
1616
NSString *const EDQueueDidDrain = @"EDQueueDidDrain";
1717

18-
NSString *const EDQueueNameKey = @"name";
19-
NSString *const EDQueueDataKey = @"data";
20-
21-
22-
NSString *const EDQueueStorageJobIdKey = @"id";
23-
NSString *const EDQueueStorageJobTaskKey = @"task";
24-
NSString *const EDQueueStorageJobDataKey = @"data";
25-
NSString *const EDQueueStorageJobAttemptsKey = @"atempts";
26-
NSString *const EDQueueStorageJobStampKey = @"stamp";
18+
static NSString *const EDQueueNameKey = @"name";
19+
static NSString *const EDQueueDataKey = @"data";
2720

2821

2922
NS_ASSUME_NONNULL_BEGIN
3023

3124
@interface EDQueue ()
32-
{
33-
BOOL _isRunning;
34-
BOOL _isActive;
35-
NSUInteger _retryLimit;
36-
}
3725

38-
@property (nonatomic) EDQueueStorageEngine *engine;
3926
@property (nonatomic, readwrite, nullable) NSString *activeTask;
4027

4128
@end
4229

43-
//
4430

4531
@implementation EDQueue
4632

47-
@synthesize isRunning = _isRunning;
48-
@synthesize isActive = _isActive;
49-
@synthesize retryLimit = _retryLimit;
50-
51-
#pragma mark - Singleton
52-
53-
+ (EDQueue *)sharedInstance
54-
{
55-
static EDQueue *singleton = nil;
56-
static dispatch_once_t once = 0;
57-
dispatch_once(&once, ^{
58-
singleton = [[self alloc] init];
59-
});
60-
return singleton;
61-
}
62-
63-
#pragma mark - Init
64-
65-
- (id)init
33+
- (instancetype)initWithPersistentStore:(id<EDQueuePersistentStorage>)persistentStore
6634
{
6735
self = [super init];
6836
if (self) {
69-
_engine = [[EDQueueStorageEngine alloc] init];
7037
_retryLimit = 4;
38+
_storage = persistentStore;
7139
}
7240
return self;
7341
}
7442

75-
- (void)dealloc
76-
{
77-
self.delegate = nil;
78-
_engine = nil;
79-
}
8043

8144
#pragma mark - Public methods
8245

@@ -90,7 +53,7 @@ - (void)dealloc
9053
*/
9154
- (void)enqueueJob:(EDQueueJob *)job
9255
{
93-
[self.engine createJob:job];
56+
[self.storage createJob:job];
9457
[self tick];
9558
}
9659

@@ -103,7 +66,7 @@ - (void)enqueueJob:(EDQueueJob *)job
10366
*/
10467
- (BOOL)jobExistsForTask:(NSString *)task
10568
{
106-
BOOL jobExists = [self.engine jobExistsForTask:task];
69+
BOOL jobExists = [self.storage jobExistsForTask:task];
10770
return jobExists;
10871
}
10972

@@ -129,7 +92,7 @@ - (BOOL)jobIsActiveForTask:(NSString *)task
12992
*/
13093
- (nullable EDQueueJob *)nextJobForTask:(NSString *)task
13194
{
132-
EDQueueJob *nextJobForTask = [self.engine fetchJobForTask:task];
95+
EDQueueJob *nextJobForTask = [self.storage fetchNextJobForTask:task];
13396
return nextJobForTask;
13497
}
13598

@@ -146,7 +109,6 @@ - (void)start
146109

147110
NSDictionary *object = @{ EDQueueNameKey : EDQueueDidStart };
148111

149-
// [self performSelectorOnMainThread:@selector(postNotificationOnMainThread:) withObject:object waitUntilDone:NO];
150112
[self postNotificationOnMainThread:object];
151113
}
152114
}
@@ -163,8 +125,6 @@ - (void)stop
163125
_isRunning = NO;
164126

165127
NSDictionary *object = @{ EDQueueNameKey : EDQueueDidStop };
166-
167-
// [self performSelectorOnMainThread:@selector(postNotification:) withObject:object waitUntilDone:NO];
168128
[self postNotificationOnMainThread:object];
169129
}
170130
}
@@ -179,7 +139,7 @@ - (void)stop
179139
*/
180140
- (void)empty
181141
{
182-
[self.engine removeAllJobs];
142+
[self.storage removeAllJobs];
183143
}
184144

185145

@@ -194,10 +154,10 @@ - (void)tick
194154
{
195155
dispatch_queue_t gcd = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
196156
dispatch_async(gcd, ^{
197-
if (self.isRunning && !self.isActive && [self.engine fetchJobCount] > 0) {
157+
if (self.isRunning && !self.isActive && [self.storage jobCount] > 0) {
198158
// Start job
199159
_isActive = YES;
200-
EDQueueJob *job = [self.engine fetchJob];
160+
EDQueueJob *job = [self.storage fetchNextJob];
201161
self.activeTask = job.task;
202162

203163
// Pass job to delegate
@@ -220,7 +180,7 @@ - (void)processJob:(EDQueueJob*)job withResult:(EDQueueResult)result
220180
EDQueueDataKey : job
221181
}];
222182

223-
[self.engine removeJob:job];
183+
[self.storage removeJob:job];
224184
break;
225185

226186
case EDQueueResultFail:
@@ -233,9 +193,9 @@ - (void)processJob:(EDQueueJob*)job withResult:(EDQueueResult)result
233193
NSUInteger currentAttempt = job.attempts.integerValue + 1;
234194

235195
if (currentAttempt < self.retryLimit) {
236-
[self.engine incrementAttemptForJob:job];
196+
[self.storage incrementAttemptForJob:job];
237197
} else {
238-
[self.engine removeJob:job];
198+
[self.storage removeJob:job];
239199
}
240200
break;
241201
case EDQueueResultCritical:
@@ -246,15 +206,15 @@ - (void)processJob:(EDQueueJob*)job withResult:(EDQueueResult)result
246206
}];
247207

248208
[self errorWithMessage:@"Critical error. Job canceled."];
249-
[self.engine removeJob:job];
209+
[self.storage removeJob:job];
250210
break;
251211
}
252212

253213
// Clean-up
254214
_isActive = NO;
255215

256216
// Drain
257-
if ([self.engine fetchJobCount] == 0) {
217+
if ([self.storage jobCount] == 0) {
258218

259219
[self postNotificationOnMainThread:@{
260220
EDQueueNameKey : EDQueueDidDrain,
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// EDQueuePersistentStorageProtocol.h
3+
// queue
4+
//
5+
// Created by Oleg Shanyuk on 19/02/16.
6+
// Copyright © 2016 DIY, Co. All rights reserved.
7+
//
8+
9+
@import Foundation;
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@class EDQueueJob;
14+
15+
@protocol EDQueuePersistentStorage <NSObject>
16+
17+
- (void)createJob:(EDQueueJob *)job;
18+
- (BOOL)jobExistsForTask:(NSString *)task;
19+
- (void)incrementAttemptForJob:(EDQueueJob *)jid;
20+
21+
- (void)removeJob:(EDQueueJob *)jid;
22+
- (void)removeAllJobs;
23+
24+
- (NSUInteger)jobCount;
25+
- (nullable EDQueueJob *)fetchNextJob;
26+
- (nullable EDQueueJob *)fetchNextJobForTask:(NSString *)task;
27+
28+
@end
29+
30+
NS_ASSUME_NONNULL_END

EDQueue/EDQueueStorageEngine.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88

99
@import Foundation;
1010

11+
#import "EDQueuePersistentStorageProtocol.h"
12+
1113
NS_ASSUME_NONNULL_BEGIN
1214

1315
@class EDQueueJob;
1416

15-
@interface EDQueueStorageEngine : NSObject
17+
@interface EDQueueStorageEngine : NSObject<EDQueuePersistentStorage>
18+
19+
- (nullable instancetype)initWithName:(NSString *)name;
20+
- (instancetype)init NS_UNAVAILABLE;
1621

17-
- (void)createJob:(EDQueueJob *)job;
18-
- (BOOL)jobExistsForTask:(NSString *)task;
19-
- (void)incrementAttemptForJob:(EDQueueJob *)jid;
20-
- (void)removeJob:(EDQueueJob *)jid;
21-
- (void)removeAllJobs;
22-
- (NSUInteger)fetchJobCount;
23-
- (nullable EDQueueJob *)fetchJob;
24-
- (nullable EDQueueJob *)fetchJobForTask:(NSString *)task;
22+
+ (void)deleteDatabaseName:(NSString *)name;
2523

2624
@end
2725

0 commit comments

Comments
 (0)