Re: NSURLConnection vs. CURLHandle vs. Others
Re: NSURLConnection vs. CURLHandle vs. Others
- Subject: Re: NSURLConnection vs. CURLHandle vs. Others
- From: Dave Hersey <email@hidden>
- Date: Wed, 03 Mar 2004 12:18:27 -0500
On 3/3/04 10:29 AM, "Edison Thomaz" <email@hidden> wrote:
>
Hi Dave,
>
>
Thanks for your feedback. I'd been thinking about using a timer, as you
>
suggest. The question to me is, what's the best way to cancel, say a
>
NSURLConnection or NSURLDownload, through a timer once the calls are in
>
progress (waiting for data that never comes for example)? It seems to
>
me that unless you do this very very well, you are going to leak
>
memory. Were you thinking about doing this in a thread and then sending
>
it an exit message if something goes wrong.
>
>
Thomaz
Thomaz,
I haven't tried this, but I would think that you could do something like the
following untested, email-quality code.
- dave
- (void) myDownloadThing
{
// ...
// Create an NSMutableURLRequest.
m_currentURLRequest = [[NSMutableURLRequest alloc] blah, blah, blah...];
// ...
// Create an NSURLDownload object.
m_currentNSURLDownload = [[NSURLDownload alloc]
initWithRequest: m_currentURLRequest
delegate:self];
// ...
}
- (void)downloadDidBegin:(NSURLDownload *)download
{
m_lastByteCount = 0;
m_bytesReceived = 0;
// Create a repeating timer for your downloadCheck: method
[NSTimer blah, blah, blah...];
}
- (void)download: (NSURLDownload *)download
didReceiveDataOfLength:(unsigned)length
{
// Bump our "bytes received" count.
m_bytesReceived += length;
}
- (void) downloadCheck:(NSTimer *)aTimer
{
// Have we received any data since the last time we fired?
if (m_lastByteCount == m_bytesReceived )
[self cancelDownload]; // No, cancel.
else
m_lastByteCount == m_bytesReceived ; // Yes, continue.
}
/*
Send your NSURLConnection or NSURLDownload instance a cancel message, and
then clean up any objects that you might have created as well as any
download file that was being created by NSURLDownload. Once the cancel
message is sent, none of the delegates will receive any more calls.
*/
- (void)cancelDownload
{
[m_downloadTimer invalidate];
m_downloadTimer = nil;
[m_currentNSURLDownload cancel];
[m_currentNSURLDownload release];
m_currentNSURLDownload = nil;
[m_currentURLRequest release];
m_currentURLRequest = nil;
// Free up our stuff and delete the download file.
// ...
}
- (void)downloadDidFinish:(NSURLDownload *)download
{
[m_downloadTimer invalidate];
m_downloadTimer = nil;
[m_currentNSURLDownload release];
m_currentNSURLDownload = nil;
[m_currentURLRequest release];
m_currentURLRequest = nil;
// ...
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
[m_downloadTimer invalidate];
m_downloadTimer = nil;
[m_currentNSURLDownload release];
m_currentNSURLDownload = nil;
[m_currentURLRequest release];
m_currentURLRequest = nil;
// ...
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.