From 7d06e2efcc6874ac0a605920fdd62ec8cc433602 Mon Sep 17 00:00:00 2001 From: rowanbeentje Date: Sat, 23 Jun 2012 00:11:30 +0000 Subject: Further improvements to further address Issue #1332: - Ensure that favourites are saved synchronously on exit to avoid background threads being killed - Improve logging on favourite rename error - Only save favourites on exit if a connection window is open --- Source/SPFavoritesController.m | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'Source/SPFavoritesController.m') diff --git a/Source/SPFavoritesController.m b/Source/SPFavoritesController.m index da8be7c6..4e1ccdef 100644 --- a/Source/SPFavoritesController.m +++ b/Source/SPFavoritesController.m @@ -35,7 +35,7 @@ static SPFavoritesController *sharedFavoritesController = nil; - (void)_loadFavorites; - (void)_constructFavoritesTree; -- (void)_saveFavoritesDataInBackground:(NSDictionary *)data; +- (void)_saveFavoritesData:(NSDictionary *)data; - (void)_addNode:(SPTreeNode *)node asChildOfNode:(SPTreeNode *)parent; - (SPTreeNode *)_constructBranchForNodeData:(NSDictionary *)nodeData; @@ -101,18 +101,27 @@ static SPFavoritesController *sharedFavoritesController = nil; * Saves the current favorites dictionary in memory to disk. Note that the current favorites data file is moved * rather than overwritten in the event that we can't write the new file, the original can simply be restored. * This method also does a lot of error checking to ensure we don't lose the user's favorites data. + * Saves the data in the background so any UI tasks can stay responsive. */ - (void)saveFavorites { pthread_mutex_lock(&favoritesLock); - [NSThread detachNewThreadSelector:@selector(_saveFavoritesDataInBackground:) + [NSThread detachNewThreadSelector:@selector(_saveFavoritesData:) toTarget:self withObject:[[[favoritesTree childNodes] objectAtIndex:0] dictionaryRepresentation]]; pthread_mutex_unlock(&favoritesLock); } +/** + * Save the current favorites dictionary in memory to disk, in the foreground, in a blocking manner. + */ +- (void)saveFavoritesSynchronously +{ + [self _saveFavoritesData:[[[favoritesTree childNodes] objectAtIndex:0] dictionaryRepresentation]]; +} + /** * Reloads the favorites data from disk with the option to save before doing so. * @@ -343,7 +352,7 @@ static SPFavoritesController *sharedFavoritesController = nil; * * @param data The raw plist data (serialized NSDictionary) to be saved */ -- (void)_saveFavoritesDataInBackground:(NSDictionary *)data +- (void)_saveFavoritesData:(NSDictionary *)data { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -411,7 +420,11 @@ static SPFavoritesController *sharedFavoritesController = nil; NSLog(@"Error writing favorites data. Restoring backup if available: %@", [error localizedDescription]); // Restore the original data file - [fileManager moveItemAtPath:favoritesBackupFile toPath:favoritesFile error:NULL]; + error = nil; + [fileManager moveItemAtPath:favoritesBackupFile toPath:favoritesFile error:&error]; + if (error) { + NSLog(@"Could not restore backup; favorites.plist left renamed as %@ due to error (%@)", favoritesBackupFile, [error localizedDescription]); + } } else { -- cgit v1.2.3