@@ -22,6 +22,7 @@ @interface SDWebImageDownloaderOperation ()
22
22
@property (assign , nonatomic ) long long expectedSize;
23
23
@property (strong , nonatomic ) NSMutableData *imageData;
24
24
@property (strong , nonatomic ) NSURLConnection *connection;
25
+ @property (strong , atomic ) NSThread *thread;
25
26
26
27
#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
27
28
@property (assign , nonatomic ) UIBackgroundTaskIdentifier backgroundTaskId;
@@ -54,34 +55,37 @@ - (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptio
54
55
55
56
- (void )start
56
57
{
57
- if (self. isCancelled )
58
+ @synchronized (self)
58
59
{
59
- self.finished = YES ;
60
- [self reset ];
61
- return ;
62
- }
60
+ if (self.isCancelled )
61
+ {
62
+ self.finished = YES ;
63
+ [self reset ];
64
+ return ;
65
+ }
63
66
64
67
#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
65
- if ([self shouldContinueWhenAppEntersBackground ])
66
- {
67
- __weak __typeof__ (self) wself = self;
68
- self.backgroundTaskId = [[UIApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^
68
+ if ([self shouldContinueWhenAppEntersBackground ])
69
69
{
70
- __strong __typeof (wself)sself = wself;
71
-
72
- if (sself)
70
+ __weak __typeof__ (self) wself = self;
71
+ self.backgroundTaskId = [[UIApplication sharedApplication ] beginBackgroundTaskWithExpirationHandler: ^
73
72
{
74
- [ sself cancel ] ;
73
+ __strong __typeof (wself) sself = wself ;
75
74
76
- [[UIApplication sharedApplication ] endBackgroundTask: sself.backgroundTaskId];
77
- sself.backgroundTaskId = UIBackgroundTaskInvalid;
78
- }
79
- }];
80
- }
75
+ if (sself)
76
+ {
77
+ [sself cancel ];
78
+
79
+ [[UIApplication sharedApplication ] endBackgroundTask: sself.backgroundTaskId];
80
+ sself.backgroundTaskId = UIBackgroundTaskInvalid;
81
+ }
82
+ }];
83
+ }
81
84
#endif
82
85
83
- self.executing = YES ;
84
- self.connection = [NSURLConnection .alloc initWithRequest: self .request delegate: self startImmediately: NO ];
86
+ self.executing = YES ;
87
+ self.connection = [NSURLConnection .alloc initWithRequest: self .request delegate: self startImmediately: NO ];
88
+ }
85
89
86
90
[self .connection start ];
87
91
@@ -121,6 +125,27 @@ - (void)start
121
125
}
122
126
123
127
- (void )cancel
128
+ {
129
+ @synchronized (self)
130
+ {
131
+ if (self.thread )
132
+ {
133
+ [self performSelector: @selector (cancelInternalAndStop ) onThread: self .thread withObject: nil waitUntilDone: NO ];
134
+ }
135
+ else
136
+ {
137
+ [self cancelInternal ];
138
+ }
139
+ }
140
+ }
141
+
142
+ - (void )cancelInternalAndStop
143
+ {
144
+ [self cancelInternal ];
145
+ CFRunLoopStop (CFRunLoopGetCurrent ());
146
+ }
147
+
148
+ - (void )cancelInternal
124
149
{
125
150
if (self.isFinished ) return ;
126
151
[super cancel ];
@@ -154,6 +179,7 @@ - (void)reset
154
179
self.progressBlock = nil ;
155
180
self.connection = nil ;
156
181
self.imageData = nil ;
182
+ self.thread = nil ;
157
183
}
158
184
159
185
- (void )setFinished : (BOOL )finished
@@ -280,7 +306,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
280
306
281
307
CFRelease (imageSource);
282
308
}
283
-
309
+
284
310
if (self.progressBlock )
285
311
{
286
312
self.progressBlock (self.imageData .length , self.expectedSize );
@@ -311,16 +337,16 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
311
337
}
312
338
else
313
339
{
314
-
340
+
315
341
UIImage *image = [UIImage sd_imageWithData: self .imageData];
316
-
342
+
317
343
image = [self scaledImageForKey: self .request.URL.absoluteString image: image];
318
-
344
+
319
345
if (!image.images ) // Do not force decod animated GIFs
320
346
{
321
347
image = [UIImage decodedImageWithImage: image];
322
348
}
323
-
349
+
324
350
if (CGSizeEqualToSize (image.size , CGSizeZero))
325
351
{
326
352
completionBlock (nil , nil , [NSError errorWithDomain: @" SDWebImageErrorDomain" code: 0 userInfo: @{NSLocalizedDescriptionKey : @" Downloaded image has 0 pixels" }], YES );
@@ -384,7 +410,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallen
384
410
[challenge.sender useCredential: [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust]
385
411
forAuthenticationChallenge: challenge];
386
412
}
387
-
413
+
388
414
[challenge.sender continueWithoutCredentialForAuthenticationChallenge: challenge];
389
415
}
390
416
0 commit comments