Skip to content

Commit a849424

Browse files
committed
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.
1 parent c17215e commit a849424

6 files changed

+35
-29
lines changed

PBGitHistoryGrapher.m

+3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ - (void) graphCommits:(NSArray *)revList
3838
if (!revList || [revList count] == 0)
3939
return;
4040

41+
NSThread *currentThread = [NSThread currentThread];
4142
NSMutableArray *commits = [NSMutableArray array];
4243
NSInteger counter = 0;
4344

4445
for (PBGitCommit *commit in revList) {
46+
if ([currentThread isCancelled])
47+
return;
4548
PBGitSHA *commitSHA = [commit sha];
4649
if (viewAllBranches || [searchSHAs containsObject:commitSHA]) {
4750
[grapher decorateCommit:commit];

PBGitHistoryList.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- (id) initWithRepository:(PBGitRepository *)repo;
4141
- (void) forceUpdate;
4242
- (void) updateHistory;
43+
- (void)cleanup;
4344

4445
- (void) updateCommitsFromGrapher:(NSDictionary *)commitData;
4546

PBGitHistoryList.m

+16-29
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ - (void) updateHistory
8282
}
8383

8484

85+
- (void)cleanup
86+
{
87+
if (currentRevList) {
88+
[currentRevList removeObserver:self forKeyPath:@"commits"];
89+
[currentRevList cancel];
90+
}
91+
[graphQueue cancelAllOperations];
92+
93+
[repository removeObserver:self forKeyPath:@"currentBranch"];
94+
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
95+
[repository removeObserver:self forKeyPath:@"hasChanged"];
96+
}
97+
98+
8599
- (NSArray *) projectCommits
86100
{
87101
return [projectRevList.commits copy];
@@ -137,12 +151,9 @@ - (void) resetGraphing
137151
resetCommits = YES;
138152
self.isUpdating = YES;
139153

140-
[graphQueue setSuspended:YES];
141-
if (graphQueue)
142-
[graphQueue removeObserver:self forKeyPath:@"operations"];
154+
[graphQueue cancelAllOperations];
143155
graphQueue = [[NSOperationQueue alloc] init];
144156
[graphQueue setMaxConcurrentOperationCount:1];
145-
[graphQueue addObserver:self forKeyPath:@"operations" options:0 context:@"operations"];
146157

147158
grapher = [self grapher];
148159
}
@@ -223,15 +234,12 @@ - (void) setCurrentRevList:(PBGitRevList *)parser
223234
if (currentRevList == parser)
224235
return;
225236

226-
if (currentRevList) {
237+
if (currentRevList)
227238
[currentRevList removeObserver:self forKeyPath:@"commits"];
228-
[currentRevList removeObserver:self forKeyPath:@"isParsing"];
229-
}
230239

231240
currentRevList = parser;
232241

233242
[currentRevList addObserver:self forKeyPath:@"commits" options:NSKeyValueObservingOptionNew context:@"commitsUpdated"];
234-
[currentRevList addObserver:self forKeyPath:@"isParsing" options:0 context:@"revListParsing"];
235243
}
236244

237245

@@ -339,22 +347,6 @@ - (void) updateHistoryForRev:(PBGitRevSpecifier *)rev
339347
#pragma mark -
340348
#pragma mark Key Value Observing
341349

342-
- (void) removeObservers
343-
{
344-
[repository removeObserver:self forKeyPath:@"currentBranch"];
345-
[repository removeObserver:self forKeyPath:@"currentBranchFilter"];
346-
[repository removeObserver:self forKeyPath:@"hasChanged"];
347-
348-
if (currentRevList) {
349-
[currentRevList removeObserver:self forKeyPath:@"commits"];
350-
[currentRevList removeObserver:self forKeyPath:@"isParsing"];
351-
}
352-
353-
if (graphQueue)
354-
[graphQueue removeObserver:self forKeyPath:@"operations"];
355-
}
356-
357-
358350
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
359351
{
360352
if ([@"currentBranch" isEqualToString:context]) {
@@ -379,11 +371,6 @@ - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(
379371
return;
380372
}
381373

382-
if ([@"revListParsing" isEqualToString:context] || [@"operations" isEqualToString:context]) {
383-
[self finishedGraphing];
384-
return;
385-
}
386-
387374
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
388375
}
389376

PBGitRepository.m

+7
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ - (void) setup
128128
revisionList = [[PBGitHistoryList alloc] initWithRepository:self];
129129
}
130130

131+
- (void)close
132+
{
133+
[revisionList cleanup];
134+
135+
[super close];
136+
}
137+
131138
- (id) initWithURL: (NSURL*) path
132139
{
133140
if (![PBGitBinary path])

PBGitRevList.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
- (id) initWithRepository:(PBGitRepository *)repo rev:(PBGitRevSpecifier *)rev shouldGraph:(BOOL)graph;
2727
- (void) loadRevisons;
28+
- (void)cancel;
2829

2930
@property (retain) NSMutableArray *commits;
3031
@property (readonly) BOOL isParsing;

PBGitRevList.mm

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ - (void) loadRevisons
5858
}
5959

6060

61+
- (void)cancel
62+
{
63+
[parseThread cancel];
64+
}
65+
66+
6167
- (void) finishedParsing
6268
{
6369
self.isParsing = NO;
@@ -217,6 +223,7 @@ - (void) walkRevisionListWithSpecifier:(PBGitRevSpecifier*)rev
217223
NSLog(@"[%@ %s] thread has been canceled", [self class], _cmd);
218224
}
219225

226+
[task terminate];
220227
[task waitUntilExit];
221228
}
222229

0 commit comments

Comments
 (0)