I currently have the following code in an NSOperation that has an observer for keyPath “isCancelled”:
downloaded = FALSE;
NSURL *url = [NSURL URLWithString:requestString];
dataXML = [[NSData alloc] initWithContentsOfURL:url];
downloaded = TRUE;
I want to make it so that the observeValueForKeyPath function is able to cancel the dataXML continuing or just completely stop the NSOperation once the NSOperation is sent a cancel message. The NSOperation’s cancelling operation cancel only notifies the operation that it should stop, but will not force my operation’s code to stop.
You can’t cancel it.
If you want to be able to cancel the load mid-way through, use
NSURLConnectionoperating in asynchronous mode. It’s a bit more work to set up but you can cancel at any point in the download process.Alternatively, you could use this handy class I wrote that wraps an async
NSURLConnectionand its delegate in a single method call 😉Easy!
</shamelessSelfPromotion>Note that the request above is already asynchronous, and the class already manages queuing of multiple requests, so you don’t need to (and shouldn’t) wrap it in an
NSOperationQueue.