Very similar to this question, I am trying to convert a project that uses ASIHTTPRequest & ASIFormDataRequest to ARC.
In my view controller classes, I often refer to and use properties of the request object in the completion blocks (looking at the response code, response data etc):
__block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];
[request setCompletionBlock:^{
if([request responseStatusCode] == 200) ....etc
When converting to ARC I get the warning:
Capturing ‘request’ strongly in this block is likely to lead to a
retain cycle
What is the proper way to do this?
Another SO user notes in the previous thread that simply adding __weak may cause the request to be released before the completion of the block, which I believe to be true.
How can I properly reference these properties in completion/failure blocks under ARC?
(I read your comment to the other question)
After implementing a few more modules using
ASIHTTPRequest, I learned that the best way was to keep astrongreference to your request object. In your case, you can do:This way you can still control
self.requesteven after you start the request (e.g. for cancelling). You can doself.request = nil;when you’re ready to release your request, maybe inside your completion block orself.request‘s parent object’s cleanup methods.Update:
If you’re targeting pre-iOS 5, then the common ground stands: use
__unsafe_unretainedinstead of__weak. This is OK because looking atASIHTTPRequest.m, the blocks arenil‘ed out in itsdealloc()(i.e. they shouldn’t get executed). Although I haven’t tested that yet, so make sure to still test with NSZombies enabled.Note:
The only safe way to cancel an
ASIHTTPRequestobject is to call itsclearDelegatesAndCancelmethod. I’ve been bitten by some nasty bugs when I was just using the plaincancelone.