1
votes

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];
}
1

1 Answers

4
votes

Here is essentially what your code is doing:

  1. Making a weak variable, weakSelf.
  2. Passing a block to a function that references that weak variable.

A this point, self is not retained by the block nor the weakSelf variable. Then, when the block runs, weakSelf is put into a __strong variable, thus retaining any value that was in weakSelf. Unfortunately, if the original value of weakSelf, namely self was already deallocated, weakSelf would be nil already.

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 __weak variable to an internal variable that happens to be __strong. This retain will happen when the block runs, not when it is created.

By using self directly 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.