Skip to content

Commit 54ad42a

Browse files
committed
Fix UI interation from background thread when adding a new table.
1 parent c37bea8 commit 54ad42a

File tree

1 file changed

+60
-33
lines changed

1 file changed

+60
-33
lines changed

Source/SPTablesList.m

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,26 @@
5555
#import <SPMySQL/SPMySQL.h>
5656

5757
// Constants
58+
//
59+
// Actions
5860
static NSString *SPAddRow = @"SPAddRow";
5961
static NSString *SPAddNewTable = @"SPAddNewTable";
6062
static NSString *SPRemoveTable = @"SPRemoveTable";
6163
static NSString *SPTruncateTable = @"SPTruncateTable";
6264
static NSString *SPDuplicateTable = @"SPDuplicateTable";
6365

66+
// New table
67+
static NSString *SPNewTableName = @"SPNewTableName";
68+
static NSString *SPNewTableType = @"SPNewTableType";
69+
static NSString *SPNewTableCharacterSet = @"SPNewTableCharacterSet";
70+
static NSString *SPNewTableCollation = @"SPNewTableCollation";
71+
6472
@interface SPTablesList () <NSSplitViewDelegate, NSTableViewDataSource>
6573

6674
- (void)_removeTable:(BOOL)force;
6775
- (void)_truncateTable;
6876
- (void)_addTable;
77+
- (void)_addTableWithDetails:(NSDictionary *)tableDetails;
6978
- (void)_copyTable;
7079
- (void)_renameTableOfType:(SPTableType)tableType from:(NSString *)oldTableName to:(NSString *)newTableName;
7180
- (void)_duplicateConnectionToFrontTab;
@@ -734,30 +743,25 @@ - (void)sheetDidEnd:(id)sheet returnCode:(NSInteger)returnCode contextInfo:(NSSt
734743
}
735744
else if ([contextInfo isEqualToString:SPRemoveTable]) {
736745
if (returnCode == NSAlertDefaultReturn) {
737-
[self _removeTable:([[(NSAlert *)sheet suppressionButton] state] == NSOnState)];
746+
[self _removeTable:[[(NSAlert *)sheet suppressionButton] state] == NSOnState];
738747
}
739748
}
740-
#ifndef SP_CODA
741749
else if ([contextInfo isEqualToString:SPTruncateTable]) {
742750
if (returnCode == NSAlertDefaultReturn) {
743751
[self _truncateTable];
744752
}
745753
}
746-
else
747-
#endif
748-
if ([contextInfo isEqualToString:SPAddNewTable]) {
754+
else if ([contextInfo isEqualToString:SPAddNewTable]) {
749755
[addTableCharsetHelper setEnabled:NO];
750756
if (returnCode == NSOKButton) {
751757
[self _addTable];
752758
}
753759
}
754-
#ifndef SP_CODA
755760
else if ([contextInfo isEqualToString:SPDuplicateTable]) {
756761
if (returnCode == NSOKButton) {
757762
[self _copyTable];
758763
}
759764
}
760-
#endif
761765
}
762766

763767
#pragma mark -
@@ -2355,25 +2359,57 @@ - (void)_truncateTable
23552359
#endif
23562360

23572361
/**
2358-
* Adds a new table table to the database using the selected character set encoding and storage engine.
2362+
* Adds a new table table to the database using the selected character set encoding and storage engine on a separate thread.
2363+
*
2364+
* This method *MUST* be called from the UI thread!
23592365
*/
23602366
- (void)_addTable
23612367
{
2362-
// Ensure the task is performed on a background thread to group addition and loads
2363-
if ([NSThread isMainThread]) {
2364-
[NSThread detachNewThreadWithName:SPCtxt(@"SPTablesList table addition task", tableDocumentInstance) target:self selector:@selector(_addTable) object:nil];
2365-
return;
2368+
NSString *tableType = [tableTypeButton title];
2369+
NSString *tableName = [tableNameField stringValue];
2370+
NSString *tableCharacterSet = [addTableCharsetHelper selectedCharset];
2371+
NSString *tableColletion = [addTableCharsetHelper selectedCollation];
2372+
2373+
NSMutableDictionary *tableDetails = [NSMutableDictionary dictionaryWithObject:tableName forKey:SPNewTableName];
2374+
2375+
if ([tableTypeButton indexOfSelectedItem] > 0) {
2376+
[tableDetails setObject:tableType forKey:SPNewTableType];
23662377
}
2367-
2368-
@autoreleasepool {
2369-
[tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), [tableNameField stringValue]]];
23702378

2379+
if (tableCharacterSet) {
2380+
[tableDetails setObject:tableCharacterSet forKey:SPNewTableCharacterSet];
2381+
}
2382+
2383+
if (tableColletion) {
2384+
[tableDetails setObject:tableColletion forKey:SPNewTableCollation];
2385+
}
2386+
2387+
[tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), tableName]];
2388+
2389+
[NSThread detachNewThreadWithName:SPCtxt(@"SPTablesList table addition task", tableDocumentInstance)
2390+
target:self
2391+
selector:@selector(_addTableWithDetails:)
2392+
object:tableDetails];
2393+
2394+
// Clear table name
2395+
[[tableNameField onMainThread] setStringValue:@""];
2396+
2397+
[tableDocumentInstance endTask];
2398+
}
2399+
2400+
/**
2401+
* Adds a new table table to the database using the selected character set encoding and storage engine.
2402+
*/
2403+
- (void)_addTableWithDetails:(NSDictionary *)tableDetails
2404+
{
2405+
@autoreleasepool
2406+
{
23712407
NSString *charSetStatement = @"";
23722408
NSString *collationStatement = @"";
23732409
NSString *engineStatement = @"";
23742410

2375-
NSString *tableType = [tableTypeButton title];
2376-
NSString *tableName = [tableNameField stringValue];
2411+
NSString *tableName = [tableDetails objectForKey:SPNewTableName];
2412+
NSString *tableType = [tableDetails objectForKey:SPNewTableType];
23772413

23782414
// Ensure the use of UTF8 when creating new tables
23792415
BOOL changeEncoding = ![[mySQLConnection encoding] isEqualToString:@"utf8"];
@@ -2384,15 +2420,17 @@ - (void)_addTable
23842420
}
23852421

23862422
// If there is an encoding selected other than the default we must specify it in CREATE TABLE statement
2387-
NSString *encodingName = [addTableCharsetHelper selectedCharset];
2423+
NSString *encodingName = [tableDetails objectForKey:SPNewTableCharacterSet];
2424+
23882425
if (encodingName) charSetStatement = [NSString stringWithFormat:@"DEFAULT CHARACTER SET %@", [encodingName backtickQuotedString]];
23892426

23902427
// If there is a collation selected other than the default we must specify it in the CREATE TABLE statement
2391-
NSString *collationName = [addTableCharsetHelper selectedCollation];
2392-
if (collationName) collationStatement = [NSString stringWithFormat:@"DEFAULT COLLATE %@",[collationName backtickQuotedString]];
2428+
NSString *collationName = [tableDetails objectForKey:SPNewTableCollation];
2429+
2430+
if (collationName) collationStatement = [NSString stringWithFormat:@"DEFAULT COLLATE %@", [collationName backtickQuotedString]];
23932431

23942432
// If there is a type selected other than the default we must specify it in CREATE TABLE statement
2395-
if ([tableTypeButton indexOfSelectedItem] > 0) {
2433+
if (tableType) {
23962434
engineStatement = [NSString stringWithFormat:@"%@ = %@", [[tableDocumentInstance serverSupport] engineTypeQueryName], [[tableDocumentInstance serverSupport] supportsQuotingEngineTypeInCreateSyntax] ? [tableType backtickQuotedString] : tableType];
23972435
}
23982436

@@ -2439,17 +2477,11 @@ - (void)_addTable
24392477
selectedTableType = SPTableTypeTable;
24402478

24412479
[[self onMainThread] updateFilter:self];
2442-
[[tablesListView onMainThread] scrollRowToVisible:[tablesListView selectedRow]];
2480+
[[tablesListView onMainThread] scrollRowToVisible:[[tablesListView onMainThread] selectedRow]];
24432481

24442482
// Select the newly created table and switch to the table structure view for easier setup
24452483
[tableDocumentInstance loadTable:selectedTableName ofType:selectedTableType];
2446-
#ifndef SP_CODA
24472484
[tableDocumentInstance viewStructure:self];
2448-
#endif
2449-
2450-
#ifdef SP_CODA
2451-
[sidebarViewController setTableNames:[self allTableNames] selectedTableName:selectedTableName];
2452-
#endif
24532485

24542486
// Query the structure of all databases in the background (mainly for completion)
24552487
[[tableDocumentInstance databaseStructureRetrieval] queryDbStructureInBackgroundWithUserInfo:@{@"forceUpdate" : @YES}];
@@ -2474,11 +2506,6 @@ - (void)_addTable
24742506

24752507
[[tablesListView onMainThread] reloadData];
24762508
}
2477-
2478-
// Clear table name
2479-
[[tableNameField onMainThread] setStringValue:@""];
2480-
2481-
[tableDocumentInstance endTask];
24822509
}
24832510
}
24842511

0 commit comments

Comments
 (0)