Skip to content

Commit 6bddfa3

Browse files
committed
1. task_id is 64 bit now
2. cleanups
1 parent bb0d09d commit 6bddfa3

File tree

2 files changed

+47
-51
lines changed

2 files changed

+47
-51
lines changed

Classes/IPOfflineQueue.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ typedef enum {
1414

1515
typedef IPOfflineQueueFilterResult (^IPOfflineQueueFilterBlock)(NSDictionary *userInfo);
1616

17+
typedef unsigned long long int task_id;
18+
1719
@class IPOfflineQueue;
1820

1921

@@ -26,7 +28,7 @@ typedef IPOfflineQueueFilterResult (^IPOfflineQueueFilterBlock)(NSDictionary *us
2628
// Returning IPOfflineQueueResultFailureShouldPauseQueue will pause the queue and the same task will be retried when the queue is resumed.
2729
// Typically, you'd only return this if the internet connection is offline or some other global condition prevents ALL queued tasks from executing.
2830
@required
29-
- (IPOfflineQueueResult)offlineQueue:(IPOfflineQueue *)queue taskId:(int)taskId executeActionWithUserInfo:(NSDictionary *)userInfo;
31+
- (IPOfflineQueueResult)offlineQueue:(IPOfflineQueue *)queue taskId:(task_id)taskId executeActionWithUserInfo:(NSDictionary *)userInfo;
3032

3133
@optional
3234
// Called before auto-resuming upon Reachability changes, app reactivation, or autoResumeInterval elapsed
@@ -48,8 +50,8 @@ typedef IPOfflineQueueFilterResult (^IPOfflineQueueFilterBlock)(NSDictionary *us
4850

4951
- (void)stop:(NSString *)reason;
5052
- (void)start:(NSString *)reason;
51-
- (void)finishTask:(int)taskId;
52-
- (void)taskFailed:(int)taskId error:(NSError *)error;
53+
- (void)finishTask:(task_id)taskId;
54+
- (void)taskFailed:(task_id)taskId error:(NSError *)error;
5355

5456
- (void)items:(void (^)(NSDictionary *userInfo))callback;
5557

Classes/IPOfflineQueue.m

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
static const int ddLogLevel = LOG_LEVEL_WARN;
1515
#endif
1616

17-
#define kMaxRetrySeconds 10000
18-
1917
static NSMutableSet *_activeQueues = nil;
2018

2119
#define TABLE_NAME @"queue2"
@@ -28,8 +26,6 @@ @interface IPOfflineQueue() {
2826

2927
NSNumber *_waitingForJob;
3028
NSDate *_waitingJobStartTime;
31-
32-
BOOL _waitingForRetry;
3329
}
3430
@end
3531

@@ -119,12 +115,6 @@ -(NSString*)dbFilePath {
119115
[NSString stringWithFormat:@"%@.queue", _name]];
120116
}
121117

122-
-(void)dropDB {
123-
[self closeDB];
124-
[[NSFileManager defaultManager] removeItemAtPath:[self dbFilePath] error:nil];
125-
[self openDB];
126-
}
127-
128118
-(void)closeDB {
129119
FMDatabaseQueue *dbQueue = self.currentDbQueue;
130120

@@ -188,7 +178,7 @@ -(void)openDB {
188178
raise];
189179
}
190180

191-
[self clear:db];
181+
[self clear];
192182
} else {
193183
isNewQueue = NO;
194184
};
@@ -246,15 +236,14 @@ -(void)enqueueOperation {
246236
}];
247237
}
248238

249-
- (void)enqueueActionWithUserInfo:(NSDictionary *)userInfo
239+
-(void)enqueueActionWithUserInfo:(NSDictionary *)userInfo
250240
{
251241
[self.dbQueue inDatabase:^(FMDatabase *db) {
252242
[self backgroundTaskBlock:^{
253243
NSMutableData *data = [[NSMutableData alloc] init];
254244
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
255245
[archiver encodeObject:userInfo forKey:@"userInfo"];
256246
[archiver finishEncoding];
257-
archiver = nil;
258247

259248
NSError *error;
260249
NSString *sql = [NSString stringWithFormat:@"INSERT INTO %@ (params) VALUES (?)", TABLE_NAME];
@@ -285,14 +274,11 @@ - (void)filterActionsUsingBlock:(IPOfflineQueueFilterBlock)filterBlock {
285274
FMResultSet *rs = [db executeQuery:[NSString stringWithFormat:@"SELECT taskid, params FROM %@ ORDER BY taskid", TABLE_NAME]];
286275

287276
while ([rs next]) {
288-
sqlite_uint64 taskId = [rs intForColumnIndex:0];
277+
sqlite_uint64 taskId = [rs unsignedLongLongIntForColumnIndex:0];
289278
NSData *blobData = [rs dataForColumnIndex:1];
290279

291-
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:blobData];
292-
NSDictionary *userInfo = [unarchiver decodeObjectForKey:@"userInfo"];
293-
[unarchiver finishDecoding];
294-
unarchiver = nil;
295-
280+
NSDictionary *userInfo = [self decodeTaskInfo:blobData];
281+
296282
if (filterBlock(userInfo) == IPOfflineQueueFilterResultAttemptToDelete) {
297283
[self deleteTask:taskId db:db];
298284
}
@@ -302,7 +288,7 @@ - (void)filterActionsUsingBlock:(IPOfflineQueueFilterBlock)filterBlock {
302288
}];
303289
}
304290

305-
- (void)clear:(FMDatabase*)db {
291+
- (void)clear {
306292
[self backgroundTaskBlock:^{
307293
[_operationQueue cancelAllOperations];
308294
[self.dbQueue inDatabase:^(FMDatabase *db) {
@@ -323,15 +309,14 @@ - (void)clear:(FMDatabase*)db {
323309
- (void)waitForRetry {
324310
NSString *reason = [NSString stringWithFormat:@"Last task of %@ failed waiting for retry", _name];
325311
DDLogError(@"Last task of %@ failed waiting for retry", _name);
326-
327-
_waitingForRetry = TRUE;
312+
328313
[self suspended:reason];
329314
}
330315

331-
- (void)waitForJob:(int)jobId {
316+
- (void)waitForJob:(task_id)jobId {
332317
_waitingForJob = [NSNumber numberWithInt:jobId];
333318
_waitingJobStartTime = [NSDate date];
334-
[self stop:[NSString stringWithFormat:@"Waiting for job id %d", jobId]];
319+
[self stop:[NSString stringWithFormat:@"Waiting for job id %lld", jobId]];
335320
}
336321

337322
- (void)stop:(NSString *)reason {
@@ -388,7 +373,11 @@ - (void)setAutoResumeInterval:(NSTimeInterval)newInterval
388373
}
389374

390375
if (newInterval > 0) {
391-
_autoResumeTimer = [NSTimer scheduledTimerWithTimeInterval:newInterval target:self selector:@selector(autoResumeTimerFired:) userInfo:nil repeats:YES];
376+
_autoResumeTimer = [NSTimer scheduledTimerWithTimeInterval:newInterval
377+
target:self
378+
selector:@selector(autoResumeTimerFired:)
379+
userInfo:nil
380+
repeats:YES];
392381
} else {
393382
_autoResumeTimer = nil;
394383
}
@@ -402,12 +391,8 @@ - (void)items:(void (^)(NSDictionary *userInfo))callback {
402391

403392
while ([rs next]) {
404393
NSData *blobData = [rs dataForColumnIndex:0];
405-
406-
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:blobData];
407-
NSDictionary *userInfo = [unarchiver decodeObjectForKey:@"userInfo"];
408-
[unarchiver finishDecoding];
409-
unarchiver = nil;
410-
394+
NSDictionary *userInfo = [self decodeTaskInfo:blobData];
395+
411396
callback(userInfo);
412397
}
413398

@@ -446,13 +431,13 @@ -(void)recoverPendingTasks {
446431

447432
// this task has failed, need to be rerun
448433
// next time the queue will run again it will pop the task again
449-
-(void)taskFailed:(int)taskId error:(NSError *)error{
450-
DDLogError(@"Task %d failed", taskId);
434+
-(void)taskFailed:(task_id)taskId error:(NSError *)error{
435+
DDLogError(@"Task %lld failed", taskId);
451436

452437
[self resetWaitingTask:taskId error:error]; // TODO run in the proper queue
453438
}
454439

455-
-(void)finishTask:(int)taskId {
440+
-(void)finishTask:(task_id)taskId {
456441
DDLogInfo(@"Task %d finished", taskId);
457442

458443
[self.dbQueue inDatabase:^(FMDatabase *db) {
@@ -461,24 +446,24 @@ -(void)finishTask:(int)taskId {
461446
}];
462447
}
463448

464-
-(void)resetWaitingTask:(int)taskId error:(NSError *)error {
465-
if ([_waitingForJob integerValue] == taskId) {
449+
-(void)resetWaitingTask:(task_id)taskId error:(NSError *)error {
450+
if ([_waitingForJob unsignedLongLongValue] == taskId) {
466451
if (error) {
467-
DDLogInfo(@"Task %d finished with error %@ total time %f seconds", taskId, error, [_waitingJobStartTime timeIntervalSinceNow]);
452+
DDLogInfo(@"Task %d finished with error %@ total time %f seconds", taskId, error, -[_waitingJobStartTime timeIntervalSinceNow]);
468453
} else {
469-
DDLogInfo(@"Task %d finished total time %f seconds", taskId, [_waitingJobStartTime timeIntervalSinceNow]);
454+
DDLogInfo(@"Task %d finished total time %f seconds", taskId, -[_waitingJobStartTime timeIntervalSinceNow]);
470455
}
471456
_waitingForJob = nil;
472457
_waitingJobStartTime = nil;
473458
}
474459
}
475460

476461

477-
-(void)deleteTask:(int)taskId db:(FMDatabase *)db {
462+
-(void)deleteTask:(task_id)taskId db:(FMDatabase *)db {
478463
[self backgroundTaskBlock:^{
479464
NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE taskid = ?", TABLE_NAME];
480465
NSError *error;
481-
BOOL deleted = [db update:sql withErrorAndBindings:&error, [NSNumber numberWithInt:taskId]];
466+
BOOL deleted = [db update:sql withErrorAndBindings:&error, [NSNumber numberWithUnsignedLongLong:taskId]];
482467

483468
if (deleted == FALSE) {
484469
[[NSException exceptionWithName:@"IPOfflineQueueDatabaseException"
@@ -494,7 +479,7 @@ -(void)deleteTask:(int)taskId db:(FMDatabase *)db {
494479
- (void)execute {
495480
[self.dbQueue inDatabase:^(FMDatabase *db) {
496481

497-
sqlite_uint64 taskId;
482+
sqlite_uint64 taskId = 0;
498483
NSData *blobData;
499484

500485
NSString *sql = [NSString stringWithFormat:@"SELECT taskid, params FROM %@ ORDER BY taskid LIMIT 1", TABLE_NAME];
@@ -509,7 +494,7 @@ - (void)execute {
509494

510495
BOOL hasData = [rs next];
511496
if (hasData) {
512-
taskId = [rs intForColumnIndex:0];
497+
taskId = [rs unsignedLongLongIntForColumnIndex:0];
513498
blobData = [rs dataForColumnIndex:1];
514499
}
515500

@@ -520,12 +505,9 @@ - (void)execute {
520505
if (isEmpty) {
521506
return;
522507
}
523-
524-
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:blobData];
525-
NSDictionary *userInfo = [unarchiver decodeObjectForKey:@"userInfo"];
526-
[unarchiver finishDecoding];
527-
unarchiver = nil;
528-
508+
509+
NSDictionary *userInfo= [self decodeTaskInfo:blobData];
510+
529511
IPOfflineQueueResult result = [self.delegate offlineQueue:self taskId:taskId executeActionWithUserInfo:userInfo];
530512
if (result == IPOfflineQueueResultSuccess) {
531513
[self deleteTask:taskId db:db];
@@ -537,4 +519,16 @@ - (void)execute {
537519
}];
538520
}
539521

522+
- (NSDictionary *)decodeTaskInfo:(NSData *)blobData {
523+
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:blobData];
524+
NSDictionary *userInfo = [unarchiver decodeObjectForKey:@"userInfo"];
525+
[unarchiver finishDecoding];
526+
#pragma clang diagnostic push
527+
#pragma ide diagnostic ignored "UnusedValue"
528+
unarchiver = nil;
529+
#pragma clang diagnostic pop
530+
531+
return userInfo;
532+
}
533+
540534
@end

0 commit comments

Comments
 (0)