From 1a2439b27b6d0c3d654eb95d42e38deaf597eb80 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Mon, 29 Aug 2011 21:40:15 +0000 Subject: - Move table creation to a threaded task, avoiding race conditions caused by loading the new table and loading the structure tab on different threads (log #2266) --- Source/SPTablesList.m | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Source/SPTablesList.m b/Source/SPTablesList.m index 9fc396d5..271b7649 100644 --- a/Source/SPTablesList.m +++ b/Source/SPTablesList.m @@ -2228,6 +2228,15 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; */ - (void)_addTable { + + // Ensure the task is performed on a background thread to group addition and loads + if ([NSThread isMainThread]) { + [NSThread detachNewThreadSelector:@selector(_addTable) toTarget:self withObject:nil]; + return; + } + NSAutoreleasePool *tableAdditionPool = [[NSAutoreleasePool alloc] init]; + [tableDocumentInstance startTaskWithDescription:[NSString stringWithFormat:NSLocalizedString(@"Creating %@...", @"Creating table task string"), [tableNameField stringValue]]]; + NSString *charSetStatement = @""; NSString *engineStatement = @""; @@ -2294,8 +2303,8 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; selectedTableName = [[NSString alloc] initWithString:tableName]; selectedTableType = SPTableTypeTable; - [self updateFilter:self]; - [tablesListView scrollRowToVisible:[tablesListView selectedRow]]; + [[self onMainThread] updateFilter:self]; + [[tablesListView onMainThread] scrollRowToVisible:[tablesListView selectedRow]]; // Select the newly created table and switch to the table structure view for easier setup [tableDocumentInstance loadTable:selectedTableName ofType:selectedTableType]; @@ -2317,11 +2326,14 @@ static NSString *SPDuplicateTable = @"SPDuplicateTable"; [NSString stringWithFormat:NSLocalizedString(@"An error occurred while trying to add the new table '%@'.\n\nMySQL said: %@", @"error adding new table informative message"), tableName, [mySQLConnection getLastErrorMessage]]); if (changeEncoding) [mySQLConnection restoreStoredEncoding]; - [tablesListView reloadData]; + [[tablesListView onMainThread] reloadData]; } // Clear table name - [tableNameField setStringValue:@""]; + [[tableNameField onMainThread] setStringValue:@""]; + + [tableDocumentInstance endTask]; + [tableAdditionPool release]; } #ifndef SP_REFACTOR -- cgit v1.2.3