Tôi đang làm việc trên mã này, hoạt động không đồng bộ dài trên mạng và khi kết thúc nó kích hoạt khối hoàn thành một số thử nghiệm được thực hiện và nếu một biến được một giá trị nhất định khác hoạt động dài nên bắt đầu ngay lập tức:Cách khắc phục "chặn" khối mạnh trong khối này có thể dẫn đến chu trình giữ lại "
-(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
}
với mã này tôi nhận được cảnh báo này từ trình biên dịch:
WARNING: Block pointer variable 'completionBlock' is uninitialized when caputerd by the block
tôi đã thay đổi:
void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request)
trong:
__block void(^completionBlock) (id obj, NSError *err, NSURLRequest *request)= ^(id obj,NSError *err, NSURLRequest *request)
nhưng tôi nhận được cảnh báo này khác:
WARNING 2: Capturing 'completionBlock' strongly in this block is likely to lead to a retain cycle
Làm thế nào tôi có thể sửa lỗi này?
Cảm ơn
Nicola
Hãy xem [answer] này (http://stackoverflow.com/questions/7761074/arc-blocks-and-retain-cycles) –