Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix two cases of "UI manipulation from background thread" (relates to #…
  • Loading branch information
dmoagx committed Oct 1, 2015
1 parent 730f1d6 commit c5878bd
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 24 deletions.
51 changes: 27 additions & 24 deletions Source/SPDataImport.m
Expand Up @@ -47,6 +47,7 @@
#import "SPFileHandle.h"
#import "SPEncodingPopupAccessory.h"
#import "SPThreadAdditions.h"
#import "SPFunctions.h"

#import <SPMySQL/SPMySQL.h>

Expand Down Expand Up @@ -401,18 +402,20 @@ - (void)importSQLFile:(NSString *)filename
BOOL useIndeterminate = NO;
if ([sqlFileHandle compressionFormat] == SPBzip2Compression) useIndeterminate = YES;

// Reset progress interface
[errorsView setString:@""];
[[singleProgressTitle onMainThread] setStringValue:NSLocalizedString(@"Importing SQL", @"text showing that the application is importing SQL")];
[[singleProgressText onMainThread] setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[[singleProgressBar onMainThread] setIndeterminate:useIndeterminate];
[[singleProgressBar onMainThread] setMaxValue:fileTotalLength];
[[singleProgressBar onMainThread] setUsesThreadedAnimation:YES];
[[singleProgressBar onMainThread] startAnimation:self];

// Open the progress sheet
[[NSApp onMainThread] beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[[singleProgressSheet onMainThread] makeKeyWindow];
SPMainQSync(^{
// Reset progress interface
[errorsView setString:@""];
[singleProgressTitle setStringValue:NSLocalizedString(@"Importing SQL", @"text showing that the application is importing SQL")];
[singleProgressText setStringValue:NSLocalizedString(@"Reading...", @"text showing that app is reading dump")];
[singleProgressBar setIndeterminate:useIndeterminate];
[singleProgressBar setMaxValue:fileTotalLength];
[singleProgressBar setUsesThreadedAnimation:YES];
[singleProgressBar startAnimation:self];

// Open the progress sheet
[NSApp beginSheet:singleProgressSheet modalForWindow:[tableDocumentInstance parentWindow] modalDelegate:self didEndSelector:nil contextInfo:nil];
[singleProgressSheet makeKeyWindow];
});

[tableDocumentInstance setQueryMode:SPImportExportQueryMode];

Expand Down Expand Up @@ -568,18 +571,18 @@ - (void)importSQLFile:(NSString *)filename
// If not set to ignore errors, ask what to do. Use NSAlert rather than
// SPBeginWaitingAlertSheet as there is already a modal sheet in progress.
if (!ignoreSQLErrors) {
NSInteger sqlImportErrorSheetReturnCode;

NSAlert *sqlErrorAlert = [NSAlert
alertWithMessageText:NSLocalizedString(@"An error occurred while importing SQL", @"sql import error message")
defaultButton:NSLocalizedString(@"Continue", @"continue button")
alternateButton:NSLocalizedString(@"Ignore All Errors", @"ignore errors button")
otherButton:NSLocalizedString(@"Stop", @"stop button")
informativeTextWithFormat:NSLocalizedString(@"[ERROR in query %ld] %@\n", @"error text when multiple custom query failed"), (long)(queriesPerformed+1), [mySQLConnection lastErrorMessage]
];
[sqlErrorAlert setAlertStyle:NSWarningAlertStyle];
sqlImportErrorSheetReturnCode = [sqlErrorAlert runModal];

__block NSInteger sqlImportErrorSheetReturnCode;

SPMainQSync(^{
NSAlert *sqlErrorAlert = [NSAlert alertWithMessageText:NSLocalizedString(@"An error occurred while importing SQL", @"sql import error message")
defaultButton:NSLocalizedString(@"Continue", @"continue button")
alternateButton:NSLocalizedString(@"Ignore All Errors", @"ignore errors button")
otherButton:NSLocalizedString(@"Stop", @"stop button")
informativeTextWithFormat:NSLocalizedString(@"[ERROR in query %ld] %@\n", @"error text when multiple custom query failed"), (long)(queriesPerformed+1), [mySQLConnection lastErrorMessage]];
[sqlErrorAlert setAlertStyle:NSWarningAlertStyle];
sqlImportErrorSheetReturnCode = [sqlErrorAlert runModal];
});
switch (sqlImportErrorSheetReturnCode) {

// On "continue", no additional action is required
Expand Down
33 changes: 33 additions & 0 deletions Source/SPFunctions.h
@@ -0,0 +1,33 @@
//
// SPFunctions.h
// sequel-pro
//
// Created by Max Lohrmann on 01.10.15.
// Copyright (c) 2015 Max Lohrmann. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// More info at <https://github.com/sequelpro/sequelpro>


void SPMainQSync(void (^block)(void));

41 changes: 41 additions & 0 deletions Source/SPFunctions.m
@@ -0,0 +1,41 @@
//
// SPFunctions.m
// sequel-pro
//
// Created by Max Lohrmann on 01.10.15.
// Copyright (c) 2015 Max Lohrmann. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// More info at <https://github.com/sequelpro/sequelpro>

#import "SPFunctions.h"

void SPMainQSync(void (^block)(void))
{
if(dispatch_get_current_queue() == dispatch_get_main_queue()) {

This comment has been minimized.

Copy link
@nacho4d

nacho4d Oct 5, 2015

Contributor

@dmoagx Probably we should use [NSThread isMainThread] instead of dispatch_get_current_queue() since it is deprecated since OSX 10.9.

block();
}
else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}
14 changes: 14 additions & 0 deletions sequel-pro.xcodeproj/project.pbxproj
Expand Up @@ -189,6 +189,7 @@
503B02D21AE95E010060CAB1 /* SPConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 173284E91088FEDE0062E892 /* SPConstants.m */; };
503CDBB21ACDC204004F8A2F /* Quartz.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 503CDBB11ACDC204004F8A2F /* Quartz.framework */; };
506CE9311A311C6C0039F736 /* SPTableContentFilterController.m in Sources */ = {isa = PBXBuildFile; fileRef = 506CE9301A311C6C0039F736 /* SPTableContentFilterController.m */; };
507FF1121BBCC57600104523 /* SPFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF1111BBCC57600104523 /* SPFunctions.m */; };
50A9F8B119EAD4B90053E571 /* SPGotoDatabaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A9F8B019EAD4B90053E571 /* SPGotoDatabaseController.m */; };
50D3C3491A75B8A800B5429C /* GotoDatabaseDialog.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50D3C34B1A75B8A800B5429C /* GotoDatabaseDialog.xib */; };
50D3C3521A77135F00B5429C /* SPParserUtils.c in Sources */ = {isa = PBXBuildFile; fileRef = 50D3C3501A77135F00B5429C /* SPParserUtils.c */; };
Expand Down Expand Up @@ -901,6 +902,8 @@
503CDBB11ACDC204004F8A2F /* Quartz.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quartz.framework; path = System/Library/Frameworks/Quartz.framework; sourceTree = SDKROOT; };
506CE92F1A311C6C0039F736 /* SPTableContentFilterController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTableContentFilterController.h; sourceTree = "<group>"; };
506CE9301A311C6C0039F736 /* SPTableContentFilterController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTableContentFilterController.m; sourceTree = "<group>"; };
507FF1101BBCC4C400104523 /* SPFunctions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPFunctions.h; sourceTree = "<group>"; };
507FF1111BBCC57600104523 /* SPFunctions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPFunctions.m; sourceTree = "<group>"; };
50A9F8AF19EAD4B90053E571 /* SPGotoDatabaseController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPGotoDatabaseController.h; sourceTree = "<group>"; };
50A9F8B019EAD4B90053E571 /* SPGotoDatabaseController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPGotoDatabaseController.m; sourceTree = "<group>"; };
50D3C34A1A75B8A800B5429C /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/GotoDatabaseDialog.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2069,6 +2072,7 @@
17E6416E0EF01F3B001BC333 /* Other */ = {
isa = PBXGroup;
children = (
507FF10E1BBCC4A900104523 /* Utility */,
1198F5B01174EDA700670590 /* Database Actions */,
583CE39511722B70008F148E /* File Compression */,
173284E51088FEC20062E892 /* Data */,
Expand Down Expand Up @@ -2369,6 +2373,15 @@
path = UnitTests;
sourceTree = "<group>";
};
507FF10E1BBCC4A900104523 /* Utility */ = {
isa = PBXGroup;
children = (
507FF1101BBCC4C400104523 /* SPFunctions.h */,
507FF1111BBCC57600104523 /* SPFunctions.m */,
);
name = Utility;
sourceTree = "<group>";
};
50D3C3591A771C2300B5429C /* Other */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -3141,6 +3154,7 @@
17E641460EF01EB5001BC333 /* main.m in Sources */,
17E641560EF01EF6001BC333 /* SPCustomQuery.m in Sources */,
17E641570EF01EF6001BC333 /* SPAppController.m in Sources */,
507FF1121BBCC57600104523 /* SPFunctions.m in Sources */,
17E641580EF01EF6001BC333 /* SPGrowlController.m in Sources */,
17E641590EF01EF6001BC333 /* SPTableContent.m in Sources */,
17E6415A0EF01EF6001BC333 /* SPDatabaseDocument.m in Sources */,
Expand Down

0 comments on commit c5878bd

Please sign in to comment.