NSURLConnection still calls the delegate AFTER the method is canceled

If you are having problems with NSURLConnection, if I create an NSURLConnection and call [connection connectionWithRequest], let it boot up a bit and then call [connection cancel] most of the time, which works fine. However, sometimes even after I call [cancel connection], the connection delegate is still called (which causes the application to crash). It seems that the problem is that the problem is the race state in runloop, I cancel the connection and release the delegate, but before the runloop loops it calls the delegate function -> crash.

Is there any way for me, after I call [connection cancel] to confirm that the connection is indeed canceled? Even crappy while () will do: (

+2
source share
2 answers

You should not release the connection and its associated storage until your delegate receives the message connectionDidFinishLoading:or connectionDidFailWithError:.

Delegates are not usually saved by the entity they act as the delegate to . However, in this case, this means that the delegate should not become invalid while NSURLConnection still refers to it, unless you overdo it.

+6
source

I have not run into this problem yet, but it may also work without binding your delegation object:

Connection , Connection ( nil), , . , "" Connection , .

- (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
{
    if(connection != _URLConnection){return;}
    ...
    [_incomingData appendData:data];
    ...
}

_URLConnection - , , nil.

0

All Articles