Skip to content

Commit

Permalink
[#14] make sure activeDelegates array is initialized and cleaned up s…
Browse files Browse the repository at this point in the history
…o that cancels actually happen
  • Loading branch information
vickeryj committed Jan 15, 2009
1 parent 267c9d4 commit 0e26ade
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 10 additions & 4 deletions Classes/lib/Connection.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ + (Response *)sendRequest:(NSMutableURLRequest *)request withUser:(NSString *)us

ConnectionDelegate *connectionDelegate = [[[ConnectionDelegate alloc] init] autorelease];

[activeDelegates addObject:connectionDelegate];
[[self activeDelegates] addObject:connectionDelegate];
NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:connectionDelegate startImmediately:NO] autorelease];
connectionDelegate.connection = connection;

Expand All @@ -76,9 +76,8 @@ + (Response *)sendRequest:(NSMutableURLRequest *)request withUser:(NSString *)us
[connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[connection scheduleInRunLoop:runLoop forMode:NSDefaultRunLoopMode];
[connection start];

while (![connectionDelegate isDone]) {
[runLoop runUntilDate:[NSDate date]];
[runLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:.5]];
}
Response *resp = [Response responseFrom:(NSHTTPURLResponse *)connectionDelegate.response
withBody:connectionDelegate.data
Expand All @@ -89,6 +88,13 @@ + (Response *)sendRequest:(NSMutableURLRequest *)request withUser:(NSString *)us
[escapedPassword release];
[activeDelegates removeObject:connectionDelegate];

//if there are no more active delegates release the array
if (0 == [activeDelegates count]) {
NSMutableArray *tempDelegates = activeDelegates;
activeDelegates = nil;
[tempDelegates release];
}

return resp;
}

Expand Down Expand Up @@ -132,7 +138,7 @@ + (Response *)delete:(NSString *)url withUser:(NSString *)user andPassword:(NSSt

+ (void) cancelAllActiveConnections {
for (ConnectionDelegate *delegate in activeDelegates) {
[delegate cancel];
[delegate performSelectorOnMainThread:@selector(cancel) withObject:nil waitUntilDone:NO];
}
}

Expand Down
3 changes: 3 additions & 0 deletions Classes/lib/ConnectionDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ - (BOOL) isDone {

- (void) cancel {
[connection cancel];
self.response = nil;
self.data = nil;
self.error = nil;
done = YES;
}

Expand Down

0 comments on commit 0e26ade

Please sign in to comment.