1
self.operationQueue = [[NSOperationQueue alloc] init];
[self.operationQueue addOperationWithBlock:^{
    [self doSomethingElse];        
}];

- (void)doSomethingElse {
    [self doAnother];
}

这会创建一个保留周期吗?我保留了对操作队列的引用,但没有引用操作。想法?

4

1 回答 1

0

这可能会创建一个保留周期。创建一个指向 self 的弱指针并使用它:

_weak MyObject *weakSelf = self;

编辑:

在你的块中,创建一个对 self 的强引用。评估您的指针以确保它有效,并且您是安全的。您的代码段(然后基于您所描述的内容)应为:

self.opeartionQueue = [[NSOperationQueue alloc] init];
_weak MyObject *weakSelf = self;
[[self operationQueue] addOperationBlock:^{

    _strong MyObject *strongSelf = weakSelf; // Obtain a strong reference so our pointer won't dangle out from under us...

    if(strongSelf) // Make sure it's valid
    {
        [strongSelf doSomethingElse]; // Do your work
    }
}
于 2012-09-14T17:22:55.567 回答