I’m having trouble figuring out why a __weak reference to self is disappearing in the following block. Following Jiva’s advice found here
http://blog.random-ideas.net/?p=160 I make a __weak reference to self outside the completion block, then make a strong reference to that weak reference within the block to hold on to it for the duration of the block.
The problem I’m having is that the weak reference (weakSelf) comes up nil when the block executes, but if I just make the call to self directly in the block everything works.
- (void)fetchItemList:(NSArray *)itemList withHandler:(ItemFetchCompletion)completionHandler
{
NSString *fetchString = [self fetchRequestUsingItemList:itemList];
NSURL *fetchUrl = [NSURL URLWithString:fetchString];
ASIHTTPRequest *itemListRequest = [ASIHTTPRequest requestWithURL:fetchUrl];
__weak FetchManager *weakSelf = self;
__weak ASIHTTPRequest *itemListRequestWeakRef = itemListRequest;
[itemListRequest setCompletionBlock:^{
ASIHTTPRequest *itemListRequestStrongRef = itemListRequestWeakRef;
ZAssert(weakSelf, @"weakSelf reference is nil.");
FetchManager *strongSelf = weakSelf;
NSData *fetchedData = [itemListRequestStrongRef responseData];
NSArray *fetchedItems = [strongSelf itemsFromFetchedData:fetchedData];
completionHandler(fetchedItems);
}];
[itemListRequest startAsynchronous];
}
Here is essentially what your code is doing:
weakSelf.A this point,
selfis not retained by the block nor theweakSelfvariable. Then, when the block runs,weakSelfis put into a__strongvariable, thus retaining any value that was inweakSelf. Unfortunately, if the original value ofweakSelf, namelyselfwas already deallocated,weakSelfwould benilalready.The compiler can see what external variables your block accesses, and then adds any necessary retains if those variables are
__strong. The compiler does not care that you assign a__weakvariable to an internal variable that happens to be__strong. This retain will happen when the block runs, not when it is created.By using
selfdirectly from the block, the compiler sees that you are referencing a variable that is not__weak, and therefore automatically retains that variable for the lifecycle of the block. In cases like this, I see no reason why you wouldn’t directly reference self in the block, unless you are worried about a circular retain cycle.