diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 53ac92f3a9..f33573d2c5 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -56,30 +56,32 @@ - (id)initWithRequest:(NSURLRequest *)request { //by default we will use the queue that created the request. self.callbackQueue = dispatch_get_current_queue(); + __weak AFHTTPRequestOperation *weakSelf = self; super.completionBlock = ^ { - if (_completionBlock) { - _completionBlock(); //call any child completion blocks that may have been passed in that they may want to run + AFHTTPRequestOperation *strongSelf = weakSelf; + if (strongSelf->_completionBlock) { + strongSelf->_completionBlock(); //call any child completion blocks that may have been passed in that they may want to run } - if ([self isCancelled]) { + if ([strongSelf isCancelled]) { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - [self endBackgroundTask]; + [strongSelf endBackgroundTask]; #endif - self.finishedBlock = nil; + strongSelf.finishedBlock = nil; return; } - if (self.finishedBlock) { - dispatch_sync(self.callbackQueue, ^(void) { - self.finishedBlock(); + if (strongSelf.finishedBlock) { + dispatch_sync(strongSelf.callbackQueue, ^(void) { + strongSelf.finishedBlock(); }); #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - [self endBackgroundTask]; + [strongSelf endBackgroundTask]; #endif - self.finishedBlock = nil; + strongSelf.finishedBlock = nil; } else { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 - [self endBackgroundTask]; + [strongSelf endBackgroundTask]; #endif } }; diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 0d7c61b522..7fd218ed23 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -256,8 +256,9 @@ - (void)setCompletionBlock:(void (^)(void))block { } else { __weak id _blockSelf = self; [super setCompletionBlock:^ { + id strongSelf = _blockSelf; block(); - [_blockSelf setCompletionBlock:nil]; + [strongSelf setCompletionBlock:nil]; }]; } }