self.operationQueue = [[NSOperationQueue alloc] init];
[self.operationQueue addOperationWithBlock:^{
[self doSomethingElse];
}];
- (void)doSomethingElse {
[self doAnother];
}
这是否会创建保留周期?我保留对操作队列的引用,但不引用操作。想法?
答案 0 :(得分:0)
这可能会创建一个保留周期。创建一个指向self的弱指针并使用它:
_weak MyObject *weakSelf = 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
}
}