我写了以下类别NSOperationBlock
@implementation NSOperationQueue (Extensions)
-(void)addAsynchronousOperationWithBlock:(void (^)(block))operationBlock
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
block signal = ^ {
dispatch_semaphore_signal(semaphore);
};
[self addOperationWithBlock:^{
operationBlock(signal);
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
}];
}
@end
它似乎工作正常,但是当我调用它时(如以下代码段所示)我收到警告:
块很可能导致一个保留周期
[_queue addAsynchronousOperationWithBlock:^(block signal) {
[self foo:nil];
signal();
}];
foo
是使用该类别的类的方法。
addOperationWithBlock:
与(from )相同的代码NSOperationQueue
不显示警告:
[_queue addOperationWithBlock:^ {
[self foo:nil];
}];
我真的不明白。特别是我不明白的是:我应该在这两种情况下都使用弱指针吗?如果我不使用弱指针,这两个片段实际上会带来一个保留周期吗?