Skip to content

Commit 053ee6d

Browse files
committed
Merge pull request #17 from jmah/jmah/mini-optimization
Optimize cancelling operations a little
2 parents bbf05de + ffd92b7 commit 053ee6d

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

Pod/Classes/PINRemoteImageManager.m

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ @interface PINRemoteImageManager () <PINURLSessionManagerDelegate>
104104
@property (nonatomic, strong) PINURLSessionManager *sessionManager;
105105
@property (nonatomic, assign) NSTimeInterval timeout;
106106
@property (nonatomic, strong) NSMutableDictionary *tasks;
107-
@property (nonatomic, strong) NSMutableArray *canceledTasks;
107+
@property (nonatomic, strong) NSMutableSet *canceledTasks;
108108
@property (nonatomic, strong) NSArray *progressThresholds;
109109
@property (nonatomic, assign) NSTimeInterval estimatedRemainingTimeThreshold;
110110
@property (nonatomic, strong) dispatch_queue_t callbackQueue;
@@ -164,7 +164,7 @@ - (instancetype)init
164164
_lowQualityBPSThreshold = 50000; // approximately edge speeds
165165
_shouldUpgradeLowQualityImages = NO;
166166
self.tasks = [[NSMutableDictionary alloc] init];
167-
self.canceledTasks = [[NSMutableArray alloc] init];
167+
self.canceledTasks = [[NSMutableSet alloc] init];
168168
self.taskQOS = [[NSMutableArray alloc] initWithCapacity:5];
169169
}
170170
return self;
@@ -886,21 +886,15 @@ - (void)cancelTaskWithUUID:(NSUUID *)UUID
886886
typeof(self) strongSelf = weakSelf;
887887
//find the task associated with the UUID. This might be spead up by storing a mapping of UUIDs to tasks
888888
[strongSelf lock];
889-
PINRemoteImageTask *taskToEvaluate = nil;
890-
NSString *taskKey = nil;
891-
for (NSString *key in [strongSelf.tasks allKeys]) {
892-
PINRemoteImageTask *task = strongSelf.tasks[key];
893-
for (NSUUID *blockUUID in [task.callbackBlocks allKeys]) {
894-
if ([blockUUID isEqual:UUID]) {
895-
taskToEvaluate = task;
896-
taskKey = key;
897-
break;
898-
}
899-
}
900-
if (taskKey) {
901-
break;
889+
__block PINRemoteImageTask *taskToEvaluate = nil;
890+
__block NSString *taskKey = nil;
891+
[strongSelf.tasks enumerateKeysAndObjectsUsingBlock:^(NSString *key, PINRemoteImageTask *task, BOOL *stop) {
892+
if (task.callbackBlocks[UUID]) {
893+
taskToEvaluate = task;
894+
taskKey = key;
895+
*stop = YES;
902896
}
903-
}
897+
}];
904898

905899
if (taskToEvaluate == nil) {
906900
//maybe task hasn't been added to task list yet, add it to canceled tasks

0 commit comments

Comments
 (0)