Skip to content

Commit 25007e0

Browse files
author
Olivier Poitrey
committed
Merge branch 'master' of git://github.com/wishabi/SDWebImage into wishabi-master
2 parents 616828b + 3f20a10 commit 25007e0

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ @interface SDWebImageDownloaderOperation ()
2222
@property (assign, nonatomic) long long expectedSize;
2323
@property (strong, nonatomic) NSMutableData *imageData;
2424
@property (strong, nonatomic) NSURLConnection *connection;
25+
@property (strong, atomic) NSThread *thread;
2526

2627
#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
2728
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundTaskId;
@@ -54,34 +55,37 @@ - (id)initWithRequest:(NSURLRequest *)request options:(SDWebImageDownloaderOptio
5455

5556
- (void)start
5657
{
57-
if (self.isCancelled)
58+
@synchronized(self)
5859
{
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+
}
6366

6467
#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])
6969
{
70-
__strong __typeof(wself)sself = wself;
71-
72-
if (sself)
70+
__weak __typeof__(self) wself = self;
71+
self.backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
7372
{
74-
[sself cancel];
73+
__strong __typeof(wself)sself = wself;
7574

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+
}
8184
#endif
8285

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+
}
8589

8690
[self.connection start];
8791

@@ -121,6 +125,27 @@ - (void)start
121125
}
122126

123127
- (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
124149
{
125150
if (self.isFinished) return;
126151
[super cancel];
@@ -154,6 +179,7 @@ - (void)reset
154179
self.progressBlock = nil;
155180
self.connection = nil;
156181
self.imageData = nil;
182+
self.thread = nil;
157183
}
158184

159185
- (void)setFinished:(BOOL)finished
@@ -280,7 +306,7 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
280306

281307
CFRelease(imageSource);
282308
}
283-
309+
284310
if (self.progressBlock)
285311
{
286312
self.progressBlock(self.imageData.length, self.expectedSize);
@@ -311,16 +337,16 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
311337
}
312338
else
313339
{
314-
340+
315341
UIImage *image = [UIImage sd_imageWithData:self.imageData];
316-
342+
317343
image = [self scaledImageForKey:self.request.URL.absoluteString image:image];
318-
344+
319345
if (!image.images) // Do not force decod animated GIFs
320346
{
321347
image = [UIImage decodedImageWithImage:image];
322348
}
323-
349+
324350
if (CGSizeEqualToSize(image.size, CGSizeZero))
325351
{
326352
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
384410
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
385411
forAuthenticationChallenge:challenge];
386412
}
387-
413+
388414
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
389415
}
390416

0 commit comments

Comments
 (0)