我正在处理这段代码,它会在网络上执行一些漫长的异步操作,并在完成后触发一个完成块,在该块中进行某些测试,如果变量获得特定值,那么另一个漫长的操作应立即开始:
-(void) performOperation
{
void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request){
int variable=0;
// Do completion operation A
//...
//...
// Do completion operation B
//Get the variable value
if(variable>0){
[self doLengthyAsynchronousOperationWithCompletionBlock: completionBlock];
}
};
//Perform the lenhgty operation with the above completionBlock
[self doLengthyAsynchronousOperationWithCompletionBlock: completionBlock];
}
-(void) doLengthyAsynchronousOperationWithCompletionBlock: completionBlock
{
//Do some lengthy asynchronous stuff
}
使用这段代码,我从编译器得到了如下警告:
WARNING: Block pointer variable 'completionBlock' is uninitialized when caputerd by the block
我改变了:
void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request)
在:
__block void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request)
但我收到了另一个警告:
WARNING 2: Capturing 'completionBlock' strongly in this block is likely to lead to a retain cycle
我该如何修复这个问题?
谢谢
Nicola