Re: NSURLConnection vs. CURLHandle vs. Others
Re: NSURLConnection vs. CURLHandle vs. Others
- Subject: Re: NSURLConnection vs. CURLHandle vs. Others
- From: Devin Lane <email@hidden>
- Date: Sun, 7 Mar 2004 16:22:52 -0800
Cocoa Programmers:
Don't forget that NSURLDownload will delete the downloaded file when
you send it the cancel message. If you want to support partial
downloading or resuming, you will need to use NSURLConnection, which
works almost exactly the same.
Another important point when using your timer to check download
activity is that NSURLConnection only calls didRecieveDataOfLength:
about once per second, meaning that your timeout should be longer than
one second. I have currently found no way of getting NSURLConnection
to send more frequent status messages.
Good luck,
Devin Lane
Cocoa Programmer
Editor-in-Chief of Tech Pep (Benson H.S. Newspaper)
Phone: (503) 775-4906
E-Mail: email@hidden
On Mar 3, 2004, at 9:18 AM, Dave Hersey wrote:
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.
_______________________________________________
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.