@@ -215,10 +215,18 @@ - (void)pauseTaskWithId: (NSString*)taskId
215
215
NSURLSessionTaskState state = download.state ;
216
216
NSString *taskIdValue = [weakSelf identifierForTask: download];
217
217
if ([taskId isEqualToString: taskIdValue] && (state == NSURLSessionTaskStateRunning )) {
218
- int64_t bytesReceived = download.countOfBytesReceived ;
219
- int64_t bytesExpectedToReceive = download.countOfBytesExpectedToReceive ;
220
- int progress = round (bytesReceived * 100 / (double )bytesExpectedToReceive);
221
218
NSDictionary *task = [weakSelf loadTaskWithId: taskIdValue];
219
+
220
+ int progress = 0 ;
221
+ if (download.countOfBytesExpectedToReceive > 0 ) {
222
+ int64_t bytesReceived = download.countOfBytesReceived ;
223
+ int64_t bytesExpectedToReceive = download.countOfBytesExpectedToReceive ;
224
+ progress = round (bytesReceived * 100 / (double )bytesExpectedToReceive);
225
+ } else {
226
+ NSNumber *progressNumOfTask = task[@" progress" ];
227
+ progress = progressNumOfTask.intValue ;
228
+ }
229
+
222
230
[download cancelByProducingResumeData: ^(NSData * _Nullable resumeData) {
223
231
// Save partial downloaded data to a file
224
232
NSFileManager *fileManager = [NSFileManager defaultManager ];
@@ -913,10 +921,19 @@ - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTas
913
921
int progress = round (totalBytesWritten * 100 / (double )totalBytesExpectedToWrite);
914
922
NSNumber *lastProgress = _runningTaskById[taskId][KEY_PROGRESS];
915
923
if (([lastProgress intValue ] == 0 || (progress > ([lastProgress intValue ] + _step)) || progress == 100 ) && progress != [lastProgress intValue ]) {
916
- [self sendUpdateProgressForTaskId: taskId inStatus: @(STATUS_RUNNING) andProgress: @(progress)];
924
+
925
+ NSNumber *status;
926
+ if (downloadTask.state == NSURLSessionTaskStateRunning ) {
927
+ status = @(STATUS_RUNNING);
928
+ } else {
929
+ NSDictionary *taskDict = [self loadTaskWithId: taskId];
930
+ status = taskDict[@" status" ];
931
+ }
932
+
933
+ [self sendUpdateProgressForTaskId: taskId inStatus: status andProgress: @(progress)];
917
934
__typeof__ (self) __weak weakSelf = self;
918
935
[self executeInDatabaseQueueForTask: ^{
919
- [weakSelf updateTask: taskId status: STATUS_RUNNING progress: progress];
936
+ [weakSelf updateTask: taskId status: status.intValue progress: progress];
920
937
}];
921
938
_runningTaskById[taskId][KEY_PROGRESS] = @(progress);
922
939
}
0 commit comments