From 59f2f3edc7e6dfa1824507afce115eb65e704704 Mon Sep 17 00:00:00 2001 From: nguyen-phillip <30534889+nguyen-phillip@users.noreply.github.com> Date: Wed, 28 Feb 2018 11:59:21 -0500 Subject: [PATCH] Check for division by zero when calculating download throughput. (#23) * Check for division by zero when calculating download throughput. * Bump version to 1.4. --- .../DownloadImageViewController.m | 17 ++++++++++++----- Restor/Info.plist | 4 ++-- restord/Info.plist | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/Restor/Controllers/ViewControllers/DownloadImageViewController.m b/Restor/Controllers/ViewControllers/DownloadImageViewController.m index 43b35f0..c5f5590 100644 --- a/Restor/Controllers/ViewControllers/DownloadImageViewController.m +++ b/Restor/Controllers/ViewControllers/DownloadImageViewController.m @@ -138,19 +138,26 @@ - (MOLAuthenticatingURLSession *)createDownloadSession { } - (void)updateProgressDescription { + NSByteCountFormatter *bf = [[NSByteCountFormatter alloc] init]; + bf.zeroPadsFractionDigits = YES; + + // Calculate throughput, while guarding against division by zero. NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate]; uint64_t currBytes = self.progress.completedUnitCount; - double throughput = (currBytes - self.startBytes) / (currTime - self.startTime); + NSTimeInterval dt = currTime - self.startTime; + NSString *throughput = @""; + if (dt > 0) { + throughput = [NSString stringWithFormat:@"(%@/sec)", + [bf stringFromByteCount:(currBytes - self.startBytes) / dt]]; + } // Update the localized description that will be displayed by DownloadImageViewController. // NSProgress's localizedAdditionalDescription can auto format this same info, but it won't zero // pad the fraction digits and updates too frequently, resulting in horrible vibrating text. - NSByteCountFormatter *bf = [[NSByteCountFormatter alloc] init]; - bf.zeroPadsFractionDigits = YES; - self.progress.localizedDescription = [NSString stringWithFormat:@"%@ of %@ (%@/sec)", + self.progress.localizedDescription = [NSString stringWithFormat:@"%@ of %@ %@", [bf stringFromByteCount:self.progress.completedUnitCount], [bf stringFromByteCount:self.progress.totalUnitCount], - [bf stringFromByteCount:throughput]]; + throughput]; // Reset values for next call. self.startTime = currTime; diff --git a/Restor/Info.plist b/Restor/Info.plist index 3a4baa9..8b5b6e0 100644 --- a/Restor/Info.plist +++ b/Restor/Info.plist @@ -15,9 +15,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.3 + 1.4 CFBundleVersion - 1.3 + 1.4 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright diff --git a/restord/Info.plist b/restord/Info.plist index de3d563..7667e4e 100644 --- a/restord/Info.plist +++ b/restord/Info.plist @@ -15,9 +15,9 @@ CFBundlePackageType APPL CFBundleShortVersionString - 1.3 + 1.4 CFBundleVersion - 4 + 5 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) NSHumanReadableCopyright