From a849424a469a576bfee00c6ee1f5297486ca4bf6 Mon Sep 17 00:00:00 2001 From: Nathan Kinsinger Date: Sat, 3 Jul 2010 18:35:04 -0600 Subject: [PATCH] Add a cleanup method to the history list. The cleanup method cancels any background threads and removes KV observers when the repository document is closed. Also removed KV observers that are no longer needed. --- PBGitHistoryGrapher.m | 3 +++ PBGitHistoryList.h | 1 + PBGitHistoryList.m | 45 +++++++++++++++---------------------------- PBGitRepository.m | 7 +++++++ PBGitRevList.h | 1 + PBGitRevList.mm | 7 +++++++ 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/PBGitHistoryGrapher.m b/PBGitHistoryGrapher.m index 891d342..65d2311 100644 --- a/PBGitHistoryGrapher.m +++ b/PBGitHistoryGrapher.m @@ -38,10 +38,13 @@ if (!revList || [revList count] == 0) return; + NSThread *currentThread = [NSThread currentThread]; NSMutableArray *commits = [NSMutableArray array]; NSInteger counter = 0; for (PBGitCommit *commit in revList) { + if ([currentThread isCancelled]) + return; PBGitSHA *commitSHA = [commit sha]; if (viewAllBranches || [searchSHAs containsObject:commitSHA]) { [grapher decorateCommit:commit]; diff --git a/PBGitHistoryList.h b/PBGitHistoryList.h index 9f00a66..2b07007 100644 --- a/PBGitHistoryList.h +++ b/PBGitHistoryList.h @@ -40,6 +40,7 @@ - (id) initWithRepository:(PBGitRepository *)repo; - (void) forceUpdate; - (void) updateHistory; +- (void)cleanup; - (void) updateCommitsFromGrapher:(NSDictionary *)commitData; diff --git a/PBGitHistoryList.m b/PBGitHistoryList.m index 017a757..a121ad1 100644 --- a/PBGitHistoryList.m +++ b/PBGitHistoryList.m @@ -82,6 +82,20 @@ } +- (void)cleanup +{ + if (currentRevList) { + [currentRevList removeObserver:self forKeyPath:@"commits"]; + [currentRevList cancel]; + } + [graphQueue cancelAllOperations]; + + [repository removeObserver:self forKeyPath:@"currentBranch"]; + [repository removeObserver:self forKeyPath:@"currentBranchFilter"]; + [repository removeObserver:self forKeyPath:@"hasChanged"]; +} + + - (NSArray *) projectCommits { return [projectRevList.commits copy]; @@ -137,12 +151,9 @@ resetCommits = YES; self.isUpdating = YES; - [graphQueue setSuspended:YES]; - if (graphQueue) - [graphQueue removeObserver:self forKeyPath:@"operations"]; + [graphQueue cancelAllOperations]; graphQueue = [[NSOperationQueue alloc] init]; [graphQueue setMaxConcurrentOperationCount:1]; - [graphQueue addObserver:self forKeyPath:@"operations" options:0 context:@"operations"]; grapher = [self grapher]; } @@ -223,15 +234,12 @@ if (currentRevList == parser) return; - if (currentRevList) { + if (currentRevList) [currentRevList removeObserver:self forKeyPath:@"commits"]; - [currentRevList removeObserver:self forKeyPath:@"isParsing"]; - } currentRevList = parser; [currentRevList addObserver:self forKeyPath:@"commits" options:NSKeyValueObservingOptionNew context:@"commitsUpdated"]; - [currentRevList addObserver:self forKeyPath:@"isParsing" options:0 context:@"revListParsing"]; } @@ -339,22 +347,6 @@ #pragma mark - #pragma mark Key Value Observing -- (void) removeObservers -{ - [repository removeObserver:self forKeyPath:@"currentBranch"]; - [repository removeObserver:self forKeyPath:@"currentBranchFilter"]; - [repository removeObserver:self forKeyPath:@"hasChanged"]; - - if (currentRevList) { - [currentRevList removeObserver:self forKeyPath:@"commits"]; - [currentRevList removeObserver:self forKeyPath:@"isParsing"]; - } - - if (graphQueue) - [graphQueue removeObserver:self forKeyPath:@"operations"]; -} - - - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([@"currentBranch" isEqualToString:context]) { @@ -379,11 +371,6 @@ return; } - if ([@"revListParsing" isEqualToString:context] || [@"operations" isEqualToString:context]) { - [self finishedGraphing]; - return; - } - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } diff --git a/PBGitRepository.m b/PBGitRepository.m index f9f9175..5b77b23 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -128,6 +128,13 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; revisionList = [[PBGitHistoryList alloc] initWithRepository:self]; } +- (void)close +{ + [revisionList cleanup]; + + [super close]; +} + - (id) initWithURL: (NSURL*) path { if (![PBGitBinary path]) diff --git a/PBGitRevList.h b/PBGitRevList.h index 7370711..0fe216d 100644 --- a/PBGitRevList.h +++ b/PBGitRevList.h @@ -25,6 +25,7 @@ - (id) initWithRepository:(PBGitRepository *)repo rev:(PBGitRevSpecifier *)rev shouldGraph:(BOOL)graph; - (void) loadRevisons; +- (void)cancel; @property (retain) NSMutableArray *commits; @property (readonly) BOOL isParsing; diff --git a/PBGitRevList.mm b/PBGitRevList.mm index ea0933d..00a1920 100644 --- a/PBGitRevList.mm +++ b/PBGitRevList.mm @@ -58,6 +58,12 @@ using namespace std; } +- (void)cancel +{ + [parseThread cancel]; +} + + - (void) finishedParsing { self.isParsing = NO; @@ -217,6 +223,7 @@ using namespace std; NSLog(@"[%@ %s] thread has been canceled", [self class], _cmd); } + [task terminate]; [task waitUntilExit]; }