English 中文(简体)
ARCA栏下的一个栏目中没有提到自我
原标题:Disappearing reference to self in a block under ARC

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.

我提出的问题是,当路障执行时,薄弱的参考线(我们微小的自治)就不起作用,但如果我只是呼吁直接控制一切。

- (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. Making a weak variable, weakSelf.
  2. Passing a block to a function that references that weak variable.

这一点,<代码>自封/代码>。 栏目和<代码>wak Self变量不予保留。 然后,在整块运行时,weak Self被装入__strong。 变量,从而保留在<代码>上的任何价值。 遗憾的是,如果<代码>weak Self,即 Self的原始价值已经处理,weak Self将改为nil

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.

通过直接使用<代码>自封/代码>,汇编者发现,你指的是一个并非<代码>wak的变量,因此自动保留该块生命周期的变量。 在类似情况下,我看不出,除非你担心循环保留周期,否则你将不直接自封。

问题回答

暂无回答




相关问题
How much overhead does a msg_send call incur?

I m attempting to piece together and run a list of tasks put together by a user. These task lists can be hundreds or thousand of items long. From what I know, the easiest and most obvious way would ...

How do Clang blocks work?

http://clang.llvm.org/docs/BlockLanguageSpec.txt Looks really cool. However, I don t understand it. I don t see examples it. I don t see examples of ideas hard to express in C++ as is, but trivial ...

Getting URL From beginSheetModalForWindow:

I m using an OpenPanel to get a file path URL. This works: [oPanel beginSheetModalForWindow:theWindow completionHandler:^(NSInteger returnCode) { NSURL *pathToFile = nil; if (returnCode == ...

结业者

我 st!

热门标签