Authored by DreamPiggy

Follow Apple's doc, add NSOperation only after all configuration done.

@@ -234,8 +234,7 @@ @@ -234,8 +234,7 @@
234 } else if (options & SDWebImageDownloaderLowPriority) { 234 } else if (options & SDWebImageDownloaderLowPriority) {
235 operation.queuePriority = NSOperationQueuePriorityLow; 235 operation.queuePriority = NSOperationQueuePriorityLow;
236 } 236 }
237 -  
238 - [sself.downloadQueue addOperation:operation]; 237 +
239 if (sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) { 238 if (sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
240 // Emulate LIFO execution order by systematically adding new operations as last operation's dependency 239 // Emulate LIFO execution order by systematically adding new operations as last operation's dependency
241 [sself.lastAddedOperation addDependency:operation]; 240 [sself.lastAddedOperation addDependency:operation];
@@ -289,6 +288,9 @@ @@ -289,6 +288,9 @@
289 UNLOCK(sself.operationsLock); 288 UNLOCK(sself.operationsLock);
290 }; 289 };
291 [self.URLOperations setObject:operation forKey:url]; 290 [self.URLOperations setObject:operation forKey:url];
  291 + // Add operation to operation queue only after all configuration done according to Apple's doc.
  292 + // `addOperation:` does not synchronously execute the `operation.completionBlock` so this will not cause deadlock.
  293 + [self.downloadQueue addOperation:operation];
292 } 294 }
293 UNLOCK(self.operationsLock); 295 UNLOCK(self.operationsLock);
294 296