Authored by 朱小军

修改原先子线程查询图片URL的方式,子线程在suboperation的外部执行 review by 于良

@@ -10,7 +10,11 @@ @@ -10,7 +10,11 @@
10 #import "SDWebImageDownloaderOperation.h" 10 #import "SDWebImageDownloaderOperation.h"
11 #import <ImageIO/ImageIO.h> 11 #import <ImageIO/ImageIO.h>
12 12
  13 +#define AliCloudHttpDNS
  14 +
  15 +#ifdef AliCloudHttpDNS
13 #import <AlicloudHttpDNS/AlicloudHttpDNS.h> 16 #import <AlicloudHttpDNS/AlicloudHttpDNS.h>
  17 +#endif
14 18
15 static NSString *const kProgressCallbackKey = @"progress"; 19 static NSString *const kProgressCallbackKey = @"progress";
16 static NSString *const kCompletedCallbackKey = @"completed"; 20 static NSString *const kCompletedCallbackKey = @"completed";
@@ -82,7 +86,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; @@ -82,7 +86,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
82 #if DEBUG 86 #if DEBUG
83 _enableHttpDNS = YES; 87 _enableHttpDNS = YES;
84 #else 88 #else
85 - _enableHttpDNS = [[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"]; 89 + _enableHttpDNS = YES;//[[[NSUserDefaults standardUserDefaults] stringForKey:@"kUserConfigHttpDNS"] isEqualToString:@"2a90dfa0f37b92aaebf369e9a4d38ba4"];
86 #endif 90 #endif
87 } 91 }
88 return self; 92 return self;
@@ -125,11 +129,12 @@ static NSString *const kCompletedCallbackKey = @"completed"; @@ -125,11 +129,12 @@ static NSString *const kCompletedCallbackKey = @"completed";
125 - (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock { 129 - (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
126 __block SDWebImageDownloaderOperation *operation; 130 __block SDWebImageDownloaderOperation *operation;
127 __weak __typeof(self)wself = self; 131 __weak __typeof(self)wself = self;
128 - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 132 +
129 BOOL useAliDNS = NO; 133 BOOL useAliDNS = NO;
130 NSString *imageURLHost = url.host;//获取图片URL的host 134 NSString *imageURLHost = url.host;//获取图片URL的host
131 NSURL *newURL = url; 135 NSURL *newURL = url;
132 - if (wself.enableHttpDNS && (imageURLHost.length > 0)) { 136 +#ifdef AliCloudHttpDNS
  137 + if (self.enableHttpDNS && (imageURLHost.length > 0)) {
133 NSArray *ips = [[HttpDnsService sharedInstance] getIpsByHostAsync:imageURLHost];//同步请求ip地址列表 138 NSArray *ips = [[HttpDnsService sharedInstance] getIpsByHostAsync:imageURLHost];//同步请求ip地址列表
134 if ([ips isKindOfClass:[NSArray class]] && [ips respondsToSelector:@selector(count)] && ips.count > 0) { 139 if ([ips isKindOfClass:[NSArray class]] && [ips respondsToSelector:@selector(count)] && ips.count > 0) {
135 NSString *ipAdress = [ips objectAtIndex:0]; 140 NSString *ipAdress = [ips objectAtIndex:0];
@@ -142,8 +147,9 @@ static NSString *const kCompletedCallbackKey = @"completed"; @@ -142,8 +147,9 @@ static NSString *const kCompletedCallbackKey = @"completed";
142 } 147 }
143 } 148 }
144 } 149 }
  150 +#endif
145 151
146 - [wself addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{ 152 + [self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^{
147 NSTimeInterval timeoutInterval = wself.downloadTimeout; 153 NSTimeInterval timeoutInterval = wself.downloadTimeout;
148 if (timeoutInterval == 0.0) { 154 if (timeoutInterval == 0.0) {
149 timeoutInterval = 15.0; 155 timeoutInterval = 15.0;
@@ -177,7 +183,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; @@ -177,7 +183,7 @@ static NSString *const kCompletedCallbackKey = @"completed";
177 } 183 }
178 } 184 }
179 completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { 185 completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
180 - // NSLog(@"download image completed:url == %@,\nimage = %@",url,image); 186 +// NSLog(@"download image completed:url == %@,\nimage = %@",url,image);
181 SDWebImageDownloader *sself = wself; 187 SDWebImageDownloader *sself = wself;
182 if (!sself) return; 188 if (!sself) return;
183 __block NSArray *callbacksForURL; 189 __block NSArray *callbacksForURL;
@@ -218,7 +224,6 @@ static NSString *const kCompletedCallbackKey = @"completed"; @@ -218,7 +224,6 @@ static NSString *const kCompletedCallbackKey = @"completed";
218 wself.lastAddedOperation = operation; 224 wself.lastAddedOperation = operation;
219 } 225 }
220 }]; 226 }];
221 - });  
222 227
223 return operation; 228 return operation;
224 } 229 }
@@ -180,15 +180,20 @@ @@ -180,15 +180,20 @@
180 // ignore image read from NSURLCache if image if cached but force refreshing 180 // ignore image read from NSURLCache if image if cached but force refreshing
181 downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse; 181 downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
182 } 182 }
  183 +
  184 + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
183 id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { 185 id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) {
184 - if (weakOperation.isCancelled) { 186 +
  187 + SDWebImageCombinedOperation *ssOperation = weakOperation;
  188 +
  189 + if (ssOperation.isCancelled) {
185 // Do nothing if the operation was cancelled 190 // Do nothing if the operation was cancelled
186 // See #699 for more details 191 // See #699 for more details
187 // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data 192 // if we would call the completedBlock, there could be a race condition between this block and another completedBlock for the same object, so if this one is called second, we will overwrite the new data
188 } 193 }
189 else if (error) { 194 else if (error) {
190 dispatch_main_sync_safe(^{ 195 dispatch_main_sync_safe(^{
191 - if (!weakOperation.isCancelled) { 196 + if (!ssOperation.isCancelled) {
192 completedBlock(nil, error, SDImageCacheTypeNone, finished, url); 197 completedBlock(nil, error, SDImageCacheTypeNone, finished, url);
193 } 198 }
194 }); 199 });
@@ -223,7 +228,7 @@ @@ -223,7 +228,7 @@
223 } 228 }
224 229
225 dispatch_main_sync_safe(^{ 230 dispatch_main_sync_safe(^{
226 - if (!weakOperation.isCancelled) { 231 + if (!ssOperation.isCancelled) {
227 completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url); 232 completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url);
228 } 233 }
229 }); 234 });
@@ -235,7 +240,7 @@ @@ -235,7 +240,7 @@
235 } 240 }
236 241
237 dispatch_main_sync_safe(^{ 242 dispatch_main_sync_safe(^{
238 - if (!weakOperation.isCancelled) { 243 + if (!ssOperation.isCancelled) {
239 completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url); 244 completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url);
240 } 245 }
241 }); 246 });
@@ -255,6 +260,8 @@ @@ -255,6 +260,8 @@
255 [self.runningOperations removeObject:weakOperation]; 260 [self.runningOperations removeObject:weakOperation];
256 } 261 }
257 }; 262 };
  263 + });
  264 +
258 } 265 }
259 else if (image) { 266 else if (image) {
260 dispatch_main_sync_safe(^{ 267 dispatch_main_sync_safe(^{