Skip to content

Commit 9d08d34

Browse files
committed
upgraded project to the more-less modern state (iOS 7 bottomline)
added nullability added EDQueueJob instead of dictionary + keys (for user data it still provides userInfo dictionary, nullable)
1 parent 6f779a5 commit 9d08d34

14 files changed

+223
-248
lines changed

EDQueue.podspec

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Pod::Spec.new do |s|
33
s.version = '0.7.2'
44
s.license = 'MIT'
55
s.summary = 'A persistent background job queue for iOS.'
6-
s.homepage = 'https://github.com/thisandagain/queue'
6+
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/thisandagain/queue.git', :tag => 'v0.7.1' }
9-
s.platform = :ios, '5.0'
8+
s.source = { :git => 'https://github.com/gelosi/queue.git', :tag => 'v0.7.2' }
9+
s.platform = :ios, '7.0'
1010
s.source_files = 'EDQueue'
1111
s.library = 'sqlite3.0'
1212
s.requires_arc = true

EDQueue/EDQueue.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#import <Foundation/Foundation.h>
1010

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

1315
typedef NS_ENUM(NSInteger, EDQueueResult) {
@@ -40,21 +42,19 @@ extern NSString *const EDQueueDataKey;
4042
@property (nonatomic, readonly) BOOL isActive;
4143
@property (nonatomic) NSUInteger retryLimit;
4244

43-
- (void)enqueueWithData:(nullable NSDictionary *)data forTask:(NSString *)task;
45+
- (void)enqueueJob:(EDQueueJob *)job;
4446
- (void)start;
4547
- (void)stop;
4648
- (void)empty;
4749

4850
- (BOOL)jobExistsForTask:(NSString *)task;
4951
- (BOOL)jobIsActiveForTask:(NSString *)task;
50-
- (NSDictionary *)nextJobForTask:(NSString *)task;
52+
- (nullable EDQueueJob *)nextJobForTask:(NSString *)task;
5153

5254
@end
5355

5456
@protocol EDQueueDelegate <NSObject>
55-
@optional
56-
- (EDQueueResult)queue:(EDQueue *)queue processJob:(NSDictionary *)job;
57-
- (void)queue:(EDQueue *)queue processJob:(NSDictionary *)job completion:(EDQueueCompletionBlock)block;
57+
- (void)queue:(EDQueue *)queue processJob:(EDQueueJob *)job completion:(EDQueueCompletionBlock)block;
5858
@end
5959

6060
NS_ASSUME_NONNULL_END

EDQueue/EDQueue.m

+21-39
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
NSString *const EDQueueNameKey = @"name";
1919
NSString *const EDQueueDataKey = @"data";
2020

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";
27+
28+
2129
NS_ASSUME_NONNULL_BEGIN
2230

2331
@interface EDQueue ()
@@ -80,10 +88,9 @@ - (void)dealloc
8088
*
8189
* @return {void}
8290
*/
83-
- (void)enqueueWithData:(nullable NSDictionary *)data forTask:(NSString *)task
91+
- (void)enqueueJob:(EDQueueJob *)job
8492
{
85-
if (data == nil) data = @{};
86-
[self.engine createJob:data forTask:task];
93+
[self.engine createJob:job];
8794
[self tick];
8895
}
8996

@@ -120,9 +127,9 @@ - (BOOL)jobIsActiveForTask:(NSString *)task
120127
*
121128
* @return {NSArray}
122129
*/
123-
- (NSDictionary *)nextJobForTask:(NSString *)task
130+
- (nullable EDQueueJob *)nextJobForTask:(NSString *)task
124131
{
125-
NSDictionary *nextJobForTask = [self.engine fetchJobForTask:task];
132+
EDQueueJob *nextJobForTask = [self.engine fetchJobForTask:task];
126133
return nextJobForTask;
127134
}
128135

@@ -190,81 +197,56 @@ - (void)tick
190197
if (self.isRunning && !self.isActive && [self.engine fetchJobCount] > 0) {
191198
// Start job
192199
_isActive = YES;
193-
NSDictionary *job = [self.engine fetchJob];
194-
self.activeTask = [job objectForKey:@"task"];
200+
EDQueueJob *job = [self.engine fetchJob];
201+
self.activeTask = job.task;
195202

196203
// Pass job to delegate
197-
if ([self.delegate respondsToSelector:@selector(queue:processJob:completion:)]) {
198204
[self.delegate queue:self processJob:job completion:^(EDQueueResult result) {
199205
[self processJob:job withResult:result];
200206
self.activeTask = nil;
201207
}];
202-
} else {
203-
EDQueueResult result = [self.delegate queue:self processJob:job];
204-
[self processJob:job withResult:result];
205-
self.activeTask = nil;
206-
}
207208
}
208209
});
209210
}
210211

211-
- (void)processJob:(NSDictionary*)job withResult:(EDQueueResult)result
212+
- (void)processJob:(EDQueueJob*)job withResult:(EDQueueResult)result
212213
{
213-
if (!job) {
214-
job = @{};
215-
}
216214
// Check result
217215
switch (result) {
218216
case EDQueueResultSuccess:
219217

220-
// NSDictionary *object = @{
221-
// EDQueueNameKey : EDQueueJobDidSucceed,
222-
// EDQueueDataKey : job
223-
// };
224-
//[NSDictionary dictionaryWithObjectsAndKeys:EDQueueJobDidSucceed, EDQueueNameKey, job, EDQueueDataKey, nil]
225-
226-
// [self performSelectorOnMainThread:@selector(postNotification:)
227-
// withObject:@{
228-
// EDQueueNameKey : EDQueueJobDidSucceed,
229-
// EDQueueDataKey : job
230-
// }
231-
// waitUntilDone:false];
232-
233218
[self postNotificationOnMainThread:@{
234219
EDQueueNameKey : EDQueueJobDidSucceed,
235220
EDQueueDataKey : job
236221
}];
237222

238-
[self.engine removeJob:[job objectForKey:@"id"]];
223+
[self.engine removeJob:job];
239224
break;
240225

241226
case EDQueueResultFail:
242-
// [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueJobDidFail, EDQueueNameKey, job, EDQueueDataKey, nil] waitUntilDone:true];
243227

244228
[self postNotificationOnMainThread:@{
245229
EDQueueNameKey : EDQueueJobDidFail,
246230
EDQueueDataKey : job
247231
}];
248232

249-
NSUInteger currentAttempt = [[job objectForKey:@"attempts"] intValue] + 1;
233+
NSUInteger currentAttempt = job.attempts.integerValue + 1;
250234

251235
if (currentAttempt < self.retryLimit) {
252-
[self.engine incrementAttemptForJob:[job objectForKey:@"id"]];
236+
[self.engine incrementAttemptForJob:job];
253237
} else {
254-
[self.engine removeJob:[job objectForKey:@"id"]];
238+
[self.engine removeJob:job];
255239
}
256240
break;
257241
case EDQueueResultCritical:
258242

259-
// [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueJobDidFail, EDQueueNameKey, job, EDQueueDataKey, nil] waitUntilDone:false];
260-
261243
[self postNotificationOnMainThread:@{
262244
EDQueueNameKey : EDQueueJobDidFail,
263245
EDQueueDataKey : job
264246
}];
265247

266248
[self errorWithMessage:@"Critical error. Job canceled."];
267-
[self.engine removeJob:[job objectForKey:@"id"]];
249+
[self.engine removeJob:job];
268250
break;
269251
}
270252

@@ -273,7 +255,7 @@ - (void)processJob:(NSDictionary*)job withResult:(EDQueueResult)result
273255

274256
// Drain
275257
if ([self.engine fetchJobCount] == 0) {
276-
// [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueDidDrain, EDQueueNameKey, nil, EDQueueDataKey, nil] waitUntilDone:false];
258+
277259
[self postNotificationOnMainThread:@{
278260
EDQueueNameKey : EDQueueDidDrain,
279261
}];

EDQueue/EDQueueJob.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// EDQueueJob.h
3+
// queue
4+
//
5+
// Created by Oleg Shanyuk on 18/02/16.
6+
// Copyright © 2016 DIY, Co. All rights reserved.
7+
//
8+
9+
@import Foundation;
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface EDQueueJob : NSObject
14+
15+
@property(nonatomic, readonly) NSString *task;
16+
@property(nonatomic, readonly) NSDictionary *userInfo;
17+
18+
@property(nonatomic, readonly, nullable) NSNumber *jobID;
19+
@property(nonatomic, readonly, nullable) NSNumber *attempts;
20+
@property(nonatomic, readonly, nullable) NSString *timeStamp;
21+
22+
- (instancetype)initWithTask:(NSString *)task
23+
userInfo:(nullable NSDictionary *)userInfo
24+
jobID:(nullable NSNumber *)jobID
25+
atempts:(nullable NSNumber *)attemps
26+
timeStamp:(nullable NSString *)timeStamp;
27+
28+
- (instancetype)initWithTask:(NSString *)task
29+
userInfo:(nullable NSDictionary *)userInfo;
30+
31+
- (instancetype)init NS_UNAVAILABLE;
32+
33+
@end
34+
35+
NS_ASSUME_NONNULL_END

EDQueue/EDQueueJob.m

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// EDQueueJob.m
3+
// queue
4+
//
5+
// Created by Oleg Shanyuk on 18/02/16.
6+
// Copyright © 2016 DIY, Co. All rights reserved.
7+
//
8+
9+
#import "EDQueueJob.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@implementation EDQueueJob
14+
15+
- (instancetype)initWithTask:(NSString *)task
16+
userInfo:(nullable NSDictionary *)userInfo
17+
jobID:(nullable NSNumber *)jobID
18+
atempts:(nullable NSNumber *)attemps
19+
timeStamp:(nullable NSString *)timeStamp
20+
{
21+
self = [super init];
22+
23+
if (self) {
24+
_jobID = [jobID copy];
25+
_task = [task copy];
26+
_userInfo = userInfo ? [userInfo copy] : @{};
27+
_attempts = [attemps copy];
28+
_timeStamp = [timeStamp copy];
29+
}
30+
31+
return self;
32+
}
33+
34+
- (instancetype)initWithTask:(NSString *)task
35+
userInfo:(nullable NSDictionary *)userInfo
36+
{
37+
return [self initWithTask:task userInfo:userInfo jobID:nil atempts:nil timeStamp:nil];
38+
}
39+
40+
@end
41+
42+
NS_ASSUME_NONNULL_END

EDQueue/EDQueueStorageEngine.h

+14-11
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66
// Copyright (c) 2012 DIY, Co. All rights reserved.
77
//
88

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

11-
@class FMDatabaseQueue;
12-
@interface EDQueueStorageEngine : NSObject
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@class EDQueueJob;
1314

14-
@property (retain) FMDatabaseQueue *queue;
15+
@interface EDQueueStorageEngine : NSObject
1516

16-
- (void)createJob:(NSDictionary *)data forTask:(id)task;
17-
- (BOOL)jobExistsForTask:(id)task;
18-
- (void)incrementAttemptForJob:(NSNumber *)jid;
19-
- (void)removeJob:(NSNumber *)jid;
17+
- (void)createJob:(EDQueueJob *)job;
18+
- (BOOL)jobExistsForTask:(NSString *)task;
19+
- (void)incrementAttemptForJob:(EDQueueJob *)jid;
20+
- (void)removeJob:(EDQueueJob *)jid;
2021
- (void)removeAllJobs;
2122
- (NSUInteger)fetchJobCount;
22-
- (NSDictionary *)fetchJob;
23-
- (NSDictionary *)fetchJobForTask:(id)task;
23+
- (nullable EDQueueJob *)fetchJob;
24+
- (nullable EDQueueJob *)fetchJobForTask:(NSString *)task;
25+
26+
@end
2427

25-
@end
28+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)