From d587521451411fe505ee7b9f7ce70aac98fb60ea Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 24 Sep 2013 13:27:37 -0700 Subject: [PATCH] [Issue #1288] Improving implementation of connection:didRecieveData: to handle case where stream -hasSpaceAvailable returns NO by polling. For in-memory buffers, this will almost never happen. [Issue #1093] Handling stream errors in connection:didRecieveData: --- AFNetworking/AFURLConnectionOperation.m | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index b71f5e8..f6e9d8d 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -569,8 +569,6 @@ static BOOL AFSecKeyIsEqualToKey(SecKeyRef key1, SecKeyRef key2) { if (![self isFinished] && self.connection) { [self.connection cancel]; - - // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:error]; } } @@ -722,9 +720,17 @@ didReceiveResponse:(NSURLResponse *)response didReceiveData:(NSData *)data { NSUInteger length = [data length]; - if ([self.outputStream hasSpaceAvailable]) { - const uint8_t *dataBuffer = (uint8_t *) [data bytes]; - [self.outputStream write:&dataBuffer[0] maxLength:length]; + while (YES) { + if ([self.outputStream hasSpaceAvailable]) { + const uint8_t *dataBuffer = (uint8_t *)[data bytes]; + if ([self.outputStream write:&dataBuffer[0] maxLength:length] == -1) { + [self.connection cancel]; + [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:self.outputStream.streamError]; + return; + } + + break; + } } dispatch_async(dispatch_get_main_queue(), ^{