Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
ensure all progress indicator action happens on main thread (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyen-phillip committed Feb 28, 2018
1 parent b1bec1e commit ac50a06
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Restor/Controllers/ViewControllers/DownloadImageViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ - (MOLAuthenticatingURLSession *)createDownloadSession {
[download writeData:d];
});

if (!self.progress) {
self.progress = [NSProgress progressWithTotalUnitCount:t.countOfBytesExpectedToReceive];
self.progress.localizedDescription = @"";
}
self.progress.completedUnitCount = t.countOfBytesReceived;
float percent = self.progress.fractionCompleted * 100;
if (receivedPercent < (int64_t)percent) {
receivedPercent = (int64_t)percent;
dispatch_async(dispatch_get_main_queue(), ^{
// Deal with all progress indicator changes on the main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.progress) {
self.progress = [NSProgress progressWithTotalUnitCount:t.countOfBytesExpectedToReceive];
self.progress.localizedDescription = @"";
}
self.progress.completedUnitCount = t.countOfBytesReceived;
float percent = self.progress.fractionCompleted * 100;
if (receivedPercent < (int64_t)percent) {
receivedPercent = (int64_t)percent;
self.percentComplete = percent;
});
}
}
});
};

authSession.taskDidCompleteWithErrorBlock = ^(NSURLSession *s, NSURLSessionTask *t, NSError *e) {
Expand Down Expand Up @@ -127,6 +128,7 @@ - (MOLAuthenticatingURLSession *)createDownloadSession {
[self dismissController:self];
});
} else {
// TODO: (Issue #19) display an error message here
NSLog(@"Error downloading image: %@", e);
[fm removeItemAtPath:downloadPath error:NULL];
}
Expand Down

0 comments on commit ac50a06

Please sign in to comment.