我正在处理这段代码,它在网络上执行一些冗长的异步操作,当它完成时会触发一个完成块,在该块中执行一些测试,如果变量获得某个值,另一个冗长的操作应该立即开始:
-(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
我怎样才能解决这个问题?
谢谢
尼古拉