diff --git a/Classes/lib/Connection.m b/Classes/lib/Connection.m index 66140af..ed730d1 100644 --- a/Classes/lib/Connection.m +++ b/Classes/lib/Connection.m @@ -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; @@ -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 @@ -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; } @@ -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]; } } diff --git a/Classes/lib/ConnectionDelegate.m b/Classes/lib/ConnectionDelegate.m index 67b301b..62f8a5a 100644 --- a/Classes/lib/ConnectionDelegate.m +++ b/Classes/lib/ConnectionDelegate.m @@ -29,6 +29,9 @@ - (BOOL) isDone { - (void) cancel { [connection cancel]; + self.response = nil; + self.data = nil; + self.error = nil; done = YES; }