-1

我有一些typedef:

typedef void (^myBlock)(SomeObject);

我有一些对象

@interface SomeObject : NSObject
@end

// Method of some arbitrary class
- (void) someMethod1 {
    SomeObject *someObject = [[SomeObject alloc] init];

    myBlock block = ^(SomeObject obj){
        // When _block(someObject)_ will be called inside someQueue - 
        // Is it guaranteed that someObject will be alive, retained inside me?       

        // Do something complex and involving (or not) obj ...    
    }

    dispatch_async(someQueue, ^{
        // Some bunch of code - after which we are sure that 
        // by the next line someMethod1 will run out, so its scope is lost

        block(someObject);
    });
}

问题放在块变量的中:是否保证我们传递给 someQueue 队列中的块块的 someObject 对象将是活动的并保留块块中?

这个问题是我刚刚提出的问题的一个更复杂的变体:将对象传递到块是否保证其生命周期将被保留?.

4

2 回答 2

1

该块及其将使用的所有内容都将保留到执行该块之后..它被捕获

于 2012-12-16T16:47:50.697 回答
0

我相信您所问的与上一个问题相同。这里唯一的区别是您不是在块内调用方法,而是在调用块。
也许我的回答不够清楚:所有内容都保留在一个块内,甚至保留一个块。

dispatch_async(someQueue, ^{
    // Some bunch of code - after which we are sure that 
    // by the next line someMethod1 will run out, so its scope is lost

    block(someObject);
});

在你调用 someObject 的块内部,所以 someObject 被捕获。块也被捕获。

于 2012-12-16T16:44:28.393 回答