Re: how to wait the finish of NSURLDownload
Re: how to wait the finish of NSURLDownload
- Subject: Re: how to wait the finish of NSURLDownload
- From: Ken Thomases <email@hidden>
- Date: Thu, 17 Apr 2008 01:41:18 -0500
On Apr 17, 2008, at 1:16 AM, xiaobin wrote:
In my application, I want to download a file from internet. so I use
NSURLDownload to do it. and using NSRunLoop to wait until finish the
downloading action.
If the file is small enough to fit into memory, you can use one of
these:
+[NSData dataWithContentsOfURL:options:error:]
+[NSURLConnection sendSynchronousRequest:returningResponse:error:]
Then, just write the data out to file with -[NSData
writeToFile:options:error:].
Alternatively, you can spawn a separate thread to perform the
NSURLDownload stuff. Use an NSConditionLock to synchronize between
the two. The main thread would create the lock, spawn the thread, and
then block waiting for the lock to change condition. The background
thread would set up the NSURLDownload and run its run loop
indefinitely. When the download completes (downloadDidFinish: or
download:didFailWithError:), it locks the lock and unlocks it with a
new condition. Then, it can call +[NSThread exit] to terminate itself.
On the other hand, you should consider if you really want/need to
synchronize on the download. Blocking your main thread for an
indefinite wait seems likely to cause poor user experience. Even if
your server is _usually_ quick to serve the file, it may sometimes be
slow -- perhaps _very_ slow -- due to forces outside your control or
the user's control.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden