18
18
NSString *const EDQueueNameKey = @" name" ;
19
19
NSString *const EDQueueDataKey = @" data" ;
20
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" ;
27
+
28
+
21
29
NS_ASSUME_NONNULL_BEGIN
22
30
23
31
@interface EDQueue ()
@@ -80,10 +88,9 @@ - (void)dealloc
80
88
*
81
89
* @return {void}
82
90
*/
83
- - (void )enqueueWithData : (nullable NSDictionary *) data forTask : ( NSString *) task
91
+ - (void )enqueueJob : (EDQueueJob *) job
84
92
{
85
- if (data == nil ) data = @{};
86
- [self .engine createJob: data forTask: task];
93
+ [self .engine createJob: job];
87
94
[self tick ];
88
95
}
89
96
@@ -120,9 +127,9 @@ - (BOOL)jobIsActiveForTask:(NSString *)task
120
127
*
121
128
* @return {NSArray}
122
129
*/
123
- - (NSDictionary *)nextJobForTask : (NSString *)task
130
+ - (nullable EDQueueJob *)nextJobForTask : (NSString *)task
124
131
{
125
- NSDictionary *nextJobForTask = [self .engine fetchJobForTask: task];
132
+ EDQueueJob *nextJobForTask = [self .engine fetchJobForTask: task];
126
133
return nextJobForTask;
127
134
}
128
135
@@ -190,81 +197,56 @@ - (void)tick
190
197
if (self.isRunning && !self.isActive && [self .engine fetchJobCount ] > 0 ) {
191
198
// Start job
192
199
_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 ;
195
202
196
203
// Pass job to delegate
197
- if ([self .delegate respondsToSelector: @selector (queue:processJob:completion: )]) {
198
204
[self .delegate queue: self processJob: job completion: ^(EDQueueResult result) {
199
205
[self processJob: job withResult: result];
200
206
self.activeTask = nil ;
201
207
}];
202
- } else {
203
- EDQueueResult result = [self .delegate queue: self processJob: job];
204
- [self processJob: job withResult: result];
205
- self.activeTask = nil ;
206
- }
207
208
}
208
209
});
209
210
}
210
211
211
- - (void )processJob : (NSDictionary *)job withResult : (EDQueueResult)result
212
+ - (void )processJob : (EDQueueJob *)job withResult : (EDQueueResult)result
212
213
{
213
- if (!job) {
214
- job = @{};
215
- }
216
214
// Check result
217
215
switch (result) {
218
216
case EDQueueResultSuccess:
219
217
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
-
233
218
[self postNotificationOnMainThread: @{
234
219
EDQueueNameKey : EDQueueJobDidSucceed,
235
220
EDQueueDataKey : job
236
221
}];
237
222
238
- [self .engine removeJob: [ job objectForKey: @" id " ] ];
223
+ [self .engine removeJob: job];
239
224
break ;
240
225
241
226
case EDQueueResultFail:
242
- // [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueJobDidFail, EDQueueNameKey, job, EDQueueDataKey, nil] waitUntilDone:true];
243
227
244
228
[self postNotificationOnMainThread: @{
245
229
EDQueueNameKey : EDQueueJobDidFail,
246
230
EDQueueDataKey : job
247
231
}];
248
232
249
- NSUInteger currentAttempt = [[ job objectForKey: @" attempts" ] intValue ] + 1 ;
233
+ NSUInteger currentAttempt = job. attempts . integerValue + 1 ;
250
234
251
235
if (currentAttempt < self.retryLimit ) {
252
- [self .engine incrementAttemptForJob: [ job objectForKey: @" id " ] ];
236
+ [self .engine incrementAttemptForJob: job];
253
237
} else {
254
- [self .engine removeJob: [ job objectForKey: @" id " ] ];
238
+ [self .engine removeJob: job];
255
239
}
256
240
break ;
257
241
case EDQueueResultCritical:
258
242
259
- // [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueJobDidFail, EDQueueNameKey, job, EDQueueDataKey, nil] waitUntilDone:false];
260
-
261
243
[self postNotificationOnMainThread: @{
262
244
EDQueueNameKey : EDQueueJobDidFail,
263
245
EDQueueDataKey : job
264
246
}];
265
247
266
248
[self errorWithMessage: @" Critical error. Job canceled." ];
267
- [self .engine removeJob: [ job objectForKey: @" id " ] ];
249
+ [self .engine removeJob: job];
268
250
break ;
269
251
}
270
252
@@ -273,7 +255,7 @@ - (void)processJob:(NSDictionary*)job withResult:(EDQueueResult)result
273
255
274
256
// Drain
275
257
if ([self .engine fetchJobCount ] == 0 ) {
276
- // [self performSelectorOnMainThread:@selector(postNotification:) withObject:[NSDictionary dictionaryWithObjectsAndKeys:EDQueueDidDrain, EDQueueNameKey, nil, EDQueueDataKey, nil] waitUntilDone:false];
258
+
277
259
[self postNotificationOnMainThread: @{
278
260
EDQueueNameKey : EDQueueDidDrain,
279
261
}];
0 commit comments