From 68e13764afdf7adfba29e891095542002d8cfe2f Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Wed, 29 Feb 2012 11:43:47 -0600 Subject: [PATCH 1/4] Added in cancel changes based on @zdzisiekpu's recommendation Changed connection to be retained, and set the connection to be nil in the cancel, success, and error case. --- AFNetworking/AFURLConnectionOperation.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 076d846..2ad446d 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -85,7 +85,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat @interface AFURLConnectionOperation () @property (readwrite, nonatomic, assign) AFOperationState state; @property (readwrite, nonatomic, retain) NSRecursiveLock *lock; -@property (readwrite, nonatomic, assign) NSURLConnection *connection; +@property (readwrite, nonatomic, retain) NSURLConnection *connection; @property (readwrite, nonatomic, retain) NSURLRequest *request; @property (readwrite, nonatomic, retain) NSURLResponse *response; @property (readwrite, nonatomic, retain) NSError *error; @@ -351,6 +351,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat - (void)cancelConnection { if (self.connection) { [self.connection cancel]; + self.connection = nil; // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; @@ -466,6 +467,8 @@ didReceiveResponse:(NSURLResponse *)response } [self finish]; + + self.connection = nil; } - (void)connection:(NSURLConnection *)__unused connection @@ -480,6 +483,8 @@ didReceiveResponse:(NSURLResponse *)response } [self finish]; + + self.connection = nil; } - (NSCachedURLResponse *)connection:(NSURLConnection *)__unused connection From 1bab351997efb6fde38dc542b6caa4dd8f04a4f5 Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Wed, 29 Feb 2012 11:57:54 -0600 Subject: [PATCH 2/4] Moved nil'ing the connection until after the delegate is called --- AFNetworking/AFURLConnectionOperation.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 2ad446d..6600d77 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -351,11 +351,12 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat - (void)cancelConnection { if (self.connection) { [self.connection cancel]; - self.connection = nil; // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]]; + + self.connection = nil; } } From 229cb926ed1e93c4a6533f7cceaa4cb25d008306 Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Wed, 29 Feb 2012 12:02:25 -0600 Subject: [PATCH 3/4] Removed nil'ing connection from the cancel process. --- AFNetworking/AFURLConnectionOperation.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 6600d77..a92db62 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -355,8 +355,6 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat // Manually send this delegate message since `[self.connection cancel]` causes the connection to never send another message to its delegate NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]]; - - self.connection = nil; } } From a3bd0cc93e0e564de04b38351102c3cc70d28a88 Mon Sep 17 00:00:00 2001 From: Kevin Harwood Date: Thu, 1 Mar 2012 08:32:56 -0600 Subject: [PATCH 4/4] Releasing the connection in dealloc --- AFNetworking/AFURLConnectionOperation.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index a92db62..f51f4eb 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -192,6 +192,8 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat [_authenticationChallenge release]; [_authenticationAgainstProtectionSpace release]; + [_connection release]; + [super dealloc]; }