Просмотр исходного кода

Fix that the minimumProgressInterval should always callback the final finished progress but not ignore it

DreamPiggy 7 лет назад
Родитель
Сommit
1d8454d

+ 2 - 2
SDWebImage/SDWebImageDownloaderConfig.h

@@ -42,13 +42,13 @@ typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
 @property (nonatomic, assign) NSTimeInterval downloadTimeout;
 @property (nonatomic, assign) NSTimeInterval downloadTimeout;
 
 
 /**
 /**
- * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value.
+ * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value. However, the final finish download progress callback does not get effected.
  * The value should be 0.0-1.0.
  * The value should be 0.0-1.0.
  * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  * @note This value may enhance the performance if you don't want progress callback too frequently.
  * @note This value may enhance the performance if you don't want progress callback too frequently.
  * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  */
  */
-@property (nonatomic, assign) NSTimeInterval minimumProgressInterval;
+@property (nonatomic, assign) double minimumProgressInterval;
 
 
 /**
 /**
  * The custom session configuration in use by NSURLSession. If you don't provide one, we will use `defaultSessionConfiguration` instead.
  * The custom session configuration in use by NSURLSession. If you don't provide one, we will use `defaultSessionConfiguration` instead.

+ 4 - 4
SDWebImage/SDWebImageDownloaderOperation.h

@@ -45,8 +45,8 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
 - (nullable NSURLSessionTask *)dataTask;
 - (nullable NSURLSessionTask *)dataTask;
 - (nullable NSURLCredential *)credential;
 - (nullable NSURLCredential *)credential;
 - (void)setCredential:(nullable NSURLCredential *)credential;
 - (void)setCredential:(nullable NSURLCredential *)credential;
-- (NSTimeInterval)minimumProgressInterval;
-- (void)setMinimumProgressInterval:(NSTimeInterval)minimumProgressInterval;
+- (double)minimumProgressInterval;
+- (void)setMinimumProgressInterval:(double)minimumProgressInterval;
 
 
 @end
 @end
 
 
@@ -76,13 +76,13 @@ FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadFinishNotification
 @property (nonatomic, strong, nullable) NSURLCredential *credential;
 @property (nonatomic, strong, nullable) NSURLCredential *credential;
 
 
 /**
 /**
- * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value.
+ * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value. However, the final finish download progress callback does not get effected.
  * The value should be 0.0-1.0.
  * The value should be 0.0-1.0.
  * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  * @note This value may enhance the performance if you don't want progress callback too frequently.
  * @note This value may enhance the performance if you don't want progress callback too frequently.
  * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  */
  */
-@property (assign, nonatomic) NSTimeInterval minimumProgressInterval;
+@property (assign, nonatomic) double minimumProgressInterval;
 
 
 /**
 /**
  * The options for the receiver.
  * The options for the receiver.

+ 2 - 5
SDWebImage/SDWebImageDownloaderOperation.m

@@ -348,8 +348,9 @@ didReceiveResponse:(NSURLResponse *)response
     // Get the current progress
     // Get the current progress
     double currentProgress = (double)self.receivedSize / (double)self.expectedSize;
     double currentProgress = (double)self.receivedSize / (double)self.expectedSize;
     double previousProgress = self.previousProgress;
     double previousProgress = self.previousProgress;
+    double progressInterval = currentProgress - previousProgress;
     // Check if we need callback progress
     // Check if we need callback progress
-    if (currentProgress - previousProgress < self.minimumProgressInterval) {
+    if (!finished && (progressInterval < self.minimumProgressInterval)) {
         return;
         return;
     }
     }
     self.previousProgress = currentProgress;
     self.previousProgress = currentProgress;
@@ -360,10 +361,6 @@ didReceiveResponse:(NSURLResponse *)response
         
         
         // progressive decode the image in coder queue
         // progressive decode the image in coder queue
         dispatch_async(self.coderQueue, ^{
         dispatch_async(self.coderQueue, ^{
-            // If all the data has already been downloaded, earily return to avoid further decoding
-            if (self.receivedSize >= self.expectedSize) {
-                return;
-            }
             UIImage *image = SDImageLoaderDecodeProgressiveImageData(imageData, self.request.URL, finished, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
             UIImage *image = SDImageLoaderDecodeProgressiveImageData(imageData, self.request.URL, finished, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
             if (image) {
             if (image) {
                 // We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.
                 // We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding.

+ 2 - 2
Tests/Tests/SDWebImageDownloaderTests.m

@@ -262,7 +262,7 @@
 - (void)test17ThatMinimumProgressIntervalWorks {
 - (void)test17ThatMinimumProgressIntervalWorks {
     XCTestExpectation *expectation = [self expectationWithDescription:@"Minimum progress interval"];
     XCTestExpectation *expectation = [self expectationWithDescription:@"Minimum progress interval"];
     SDWebImageDownloaderConfig *config = SDWebImageDownloaderConfig.defaultDownloaderConfig;
     SDWebImageDownloaderConfig *config = SDWebImageDownloaderConfig.defaultDownloaderConfig;
-    config.minimumProgressInterval = 0.51; // This will make the progress only callback once
+    config.minimumProgressInterval = 0.51; // This will make the progress only callback twice (once is 51%, another is 100%)
     SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] initWithConfig:config];
     SDWebImageDownloader *downloader = [[SDWebImageDownloader alloc] initWithConfig:config];
     NSURL *imageURL = [NSURL URLWithString:@"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp"];
     NSURL *imageURL = [NSURL URLWithString:@"http://www.ioncannon.net/wp-content/uploads/2011/06/test2.webp"];
     __block NSUInteger allProgressCount = 0; // All progress (including operation start / first HTTP response, etc)
     __block NSUInteger allProgressCount = 0; // All progress (including operation start / first HTTP response, etc)
@@ -275,7 +275,7 @@
         }
         }
         validProgressCount++;
         validProgressCount++;
     } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
     } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
-        if (allProgressCount > 1 && validProgressCount == 1) {
+        if (allProgressCount > 2 && validProgressCount == 2) {
             [expectation fulfill];
             [expectation fulfill];
         } else {
         } else {
             XCTFail(@"Progress callback more than once");
             XCTFail(@"Progress callback more than once");