From 865dd2e187ea2c5fe153f3124b468b35b4d26cc1 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 29 Dec 2017 19:27:56 +0000 Subject: [PATCH] Ensure UI is changed only on the main thread --- Sparkle/SUBasicUpdateDriver.m | 6 +-- Sparkle/SUUIBasedUpdateDriver.m | 69 +++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index ff0f7126..e62721c7 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -196,10 +196,10 @@ if ([self itemContainsValidUpdate:item]) { self.updateItem = item; - [self didFindValidUpdate]; + [self performSelectorOnMainThread:@selector(didFindValidUpdate) withObject:nil waitUntilDone:NO]; } else { self.updateItem = nil; - [self didNotFindUpdate]; + [self performSelectorOnMainThread:@selector(didNotFindUpdate) withObject:nil waitUntilDone:NO]; } } @@ -374,7 +374,7 @@ return; } - [self unarchiverDidFinish:nil]; + [self performSelectorOnMainThread:@selector(unarchiverDidFinish:) withObject:nil waitUntilDone:NO]; } progressBlock:^(double progress) { [self unarchiver:nil extractedProgress:progress]; }]; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 2aa592bd..cfcbf36a 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -171,7 +171,9 @@ - (void)downloaderDidReceiveExpectedContentLength:(int64_t) expectedContentLength { - [self.statusController setMaxProgressValue:expectedContentLength > 0 ? expectedContentLength : self.updateItem.contentLength]; + dispatch_async(dispatch_get_main_queue(), ^{ + [self.statusController setMaxProgressValue:expectedContentLength > 0 ? expectedContentLength : self.updateItem.contentLength]; + }); } - (NSString *)localizedStringFromByteCount:(long long)value @@ -207,18 +209,21 @@ - (void)downloaderDidReceiveDataOfLength:(uint64_t) length { - double newProgressValue = [self.statusController progressValue] + (double)length; - - // In case our expected content length was incorrect - if (newProgressValue > [self.statusController maxProgressValue]) { - [self.statusController setMaxProgressValue:newProgressValue]; - } - - [self.statusController setProgressValue:newProgressValue]; - if ([self.statusController maxProgressValue] > 0.0) - [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ of %@", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue], [self localizedStringFromByteCount:(long long)self.statusController.maxProgressValue]]]; - else - [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue]]]; + dispatch_async(dispatch_get_main_queue(), ^{ + double newProgressValue = [self.statusController progressValue] + (double)length; + + // In case our expected content length was incorrect + if (newProgressValue > [self.statusController maxProgressValue]) { + [self.statusController setMaxProgressValue:newProgressValue]; + } + + [self.statusController setProgressValue:newProgressValue]; + if ([self.statusController maxProgressValue] > 0.0) { + [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ of %@", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue], [self localizedStringFromByteCount:(long long)self.statusController.maxProgressValue]]]; + } else { + [self.statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self localizedStringFromByteCount:(long long)self.statusController.progressValue]]]; + } + }); } - (IBAction)cancelDownload:(id)__unused sender @@ -236,19 +241,23 @@ - (void)extractUpdate { - // Now we have to extract the downloaded archive. - [self.statusController beginActionWithTitle:SULocalizedString(@"Extracting update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; - [self.statusController setButtonEnabled:NO]; + dispatch_sync(dispatch_get_main_queue(), ^{ + // Now we have to extract the downloaded archive. + [self.statusController beginActionWithTitle:SULocalizedString(@"Extracting update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; + [self.statusController setButtonEnabled:NO]; + }); [super extractUpdate]; } - (void)unarchiver:(id)__unused ua extractedProgress:(double)progress { - // We do this here instead of in extractUpdate so that we only have a determinate progress bar for archives with progress. - if ([self.statusController maxProgressValue] == 0.0) { - [self.statusController setMaxProgressValue:1]; - } - [self.statusController setProgressValue:progress]; + dispatch_async(dispatch_get_main_queue(), ^{ + // We do this here instead of in extractUpdate so that we only have a determinate progress bar for archives with progress. + if ([self.statusController maxProgressValue] == 0.0) { + [self.statusController setMaxProgressValue:1]; + } + [self.statusController setProgressValue:progress]; + }); } - (void)unarchiverDidFinish:(id)__unused ua @@ -295,14 +304,16 @@ - (void)abortUpdateWithError:(NSError *)error { - if (self.showErrors) { - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = SULocalizedString(@"Update Error!", nil); - alert.informativeText = [NSString stringWithFormat:@"%@", [error localizedDescription]]; - [alert addButtonWithTitle:SULocalizedString(@"Cancel Update", nil)]; - [self showAlert:alert]; - } - [super abortUpdateWithError:error]; + dispatch_sync(dispatch_get_main_queue(), ^{ + if (self.showErrors) { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = SULocalizedString(@"Update Error!", nil); + alert.informativeText = [NSString stringWithFormat:@"%@", [error localizedDescription]]; + [alert addButtonWithTitle:SULocalizedString(@"Cancel Update", nil)]; + [self showAlert:alert]; + } + [super abortUpdateWithError:error]; + }); } - (void)abortUpdate