From ac50a065fc0f1241327e863ce5f601dfea6149f5 Mon Sep 17 00:00:00 2001 From: nguyen-phillip <30534889+nguyen-phillip@users.noreply.github.com> Date: Wed, 28 Feb 2018 10:14:39 -0500 Subject: [PATCH] ensure all progress indicator action happens on main thread (#21) --- .../DownloadImageViewController.m | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Restor/Controllers/ViewControllers/DownloadImageViewController.m b/Restor/Controllers/ViewControllers/DownloadImageViewController.m index 3e63475..43b35f0 100644 --- a/Restor/Controllers/ViewControllers/DownloadImageViewController.m +++ b/Restor/Controllers/ViewControllers/DownloadImageViewController.m @@ -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) { @@ -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]; }