diff --git a/Libraries/Image/RCTImageLoader.m b/Libraries/Image/RCTImageLoader.m index 4f70fd0b9b4232..b8edc9148a980c 100644 --- a/Libraries/Image/RCTImageLoader.m +++ b/Libraries/Image/RCTImageLoader.m @@ -236,10 +236,14 @@ - (void)dequeueTasks { dispatch_async(_URLRequestQueue, ^{ // Remove completed tasks + NSMutableArray *tasksToRemove = nil; for (RCTNetworkTask *task in self->_pendingTasks.reverseObjectEnumerator) { switch (task.status) { case RCTNetworkTaskFinished: - [self->_pendingTasks removeObject:task]; + if (!tasksToRemove) { + tasksToRemove = [NSMutableArray new]; + } + [tasksToRemove addObject:task]; self->_activeTasks--; break; case RCTNetworkTaskPending: @@ -248,13 +252,20 @@ - (void)dequeueTasks // Check task isn't "stuck" if (task.requestToken == nil) { RCTLogWarn(@"Task orphaned for request %@", task.request); - [self->_pendingTasks removeObject:task]; + if (!tasksToRemove) { + tasksToRemove = [NSMutableArray new]; + } + [tasksToRemove addObject:task]; self->_activeTasks--; [task cancel]; } break; } } + + if (tasksToRemove) { + [self->_pendingTasks removeObjectsInArray:tasksToRemove]; + } // Start queued decode NSInteger activeDecodes = self->_scheduledDecodes - self->_pendingDecodes.count;