Canceling the calling instance method in NSURLConnection most often does not cancel the connection at all.
I am performing an asynchronous NSURLConnection in NSOperation. If the operation is canceled, the connection is terminated, causing the cancellation in the NSURLConnection, and then establishing the connection to zero.
Is there a way to cancel the connection immediately? Most often, it continues to run in the background until the request is complete, even if the connection is canceled and set to zero. The NSOperation subclass is freed after the connection is canceled when the connection is dropped. Even after that, the asynchronous connection continues to work!
Is there any other way to undo it? Or is it possible to cancel its stream?
Here is the code snippet from the main method of the NSOperation subclass:
// Create and start asynchronous connection self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; [request release]; while(!isFinished) { if(self.isCancelled) { [self.connection cancel]; self.connection = nil; [self uploadCanceledDelegate]; return; } // Special sleep because callbacks are executed on same thread as this loop [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; }
NOTE. . This behavior only occurs when WiFi is turned on. It works great with 3G / Edge / GPRS
source share