< Back

iOS Pain Point of the Day: Using __weak with ASIFormDataRequest May Cause the Request to be Preemptively Released

After a multitude of failed builds and a thorough shaming of TestFlight, Apple and ultimately myself, I finally tracked down my ASIHTTPRequest problem.

Using __weak with blocks for the ASIFormDataRequest object “may cause the request to be released before the completion of the block” as referenced here. In my case, it certainly caused an early release thus never triggering a completed nor failed block.

How it happened

ARC was the genesis issue. It apparently really hates potential retain cycles. Here’s the specific warning:

Capturing ‘request’ strongly in this block is likely to lead to a retain cycle

So, I simply swapped out __block for __weak and my local development environment tested perfectly. The problem happened only when I distributed the app through TestFlight to my device, and after hours of console debugging, I couldn’t nail down the issue.

What to do

As the commenter astutely points out in the stack overflow post, the best way to solve this dilemma is to keep a strong reference to your request object. He lays out the following syntax describing it:

self.request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]];
__weak ASIFormDataRequest *weakRequest = self.request; // __block directive not needed since we only access the instance's properties. 
[self.request setCompletionBlock:^{

    if([weakRequest responseStatusCode] == 200)
    // ...

Hopefully, this will save you from a headache.

Connect with us

Thank You!

We really appreciate your interest in what we do.

We'll get back to you as soon as we can.