Re: Asynchronous downloading again
Re: Asynchronous downloading again
- Subject: Re: Asynchronous downloading again
- From: Ken Thomases <email@hidden>
- Date: Sun, 1 Nov 2009 01:25:42 -0500
On Oct 31, 2009, at 11:19 PM, DKJ wrote:
The 2-file example was a simplified case I posted when I was asking
whether the method would work. I actually have half-a-dozen or so
data files to download. (It can vary.)
I'm using the delegate to save the files to disk under different
names, so the delegate has to know which filename to use for which
connection. There's even a file that needs to be downloaded
containing the names of other files that need to be downloaded.
Any suggestions about how to do all of this with multiple
NSURLConnections would be quite welcome. Elegant code is a
worthwhile end in itself, even if there is no practical benefit in a
particular case.
There are a lot of approaches to this problem. Here's one quick-and-
dirty approach:
Have a single object which will serve as the delegate for all of the
downloads. It will actually be a controller for the whole process
covering all of the downloads, so it knows what needs to be done when,
in what order, how to use data from one download to initiate another,
and when it's all finished.
It will have a mutable dictionary mapping from NSURLConnection
instances to a bit of information you need to decide what to do when
the download completes. (NSURLConnection can't act as a dictionary
key because it's not copyable. You can use +[NSValue
valueWithNonretainedObject:] as the keys or use NSMapTable instead of
NSMutableDictionary.)
When delegate methods are invoked, you look up the information you
need to decide what to do. Which specific information is required for
the delegate to make that decision is for you to design. You could
make it a dictionary with some data and then code your logic right
there in the delegate method. Or, you could make one value in the
dictionary a selector string for another method to invoke on the
delegate, passing some other values from the dictionary as arguments.
The action taken as a result of an event on a connection may involve
adding new connections to the delegate's mapping table.
If the delegate method is one which indicates that the connection
attempt has completed (connectionDidFail/Finish...), then that
connection is removed from the table.
If there are no more connections in the table, then everything is
complete and the delegate can take whatever next step is appropriate.
If there are more connections, the delegate can start some or all of
them. (While running multiple simultaneous downloads is probably a
good thing, you probably want to put some upper limit on the number
running at once. The delegate, being the overall controller of the
downloads, would be in control of that.)
A more refined, less quick-and-dirty approach would be to create a
custom class or two for the different types of downloads you perform.
Each instance of those classes would own a single NSURLConnection and
would be its delegate. The classes would encapsulate the information
and logic to know what to do with its downloaded data. For example,
there'd be one class for downloading the list of other files to
download. That one would create instances of another class, which
would know about downloading an individual file.
There might still be a controller to manage the overall process. It
might limit the total number of simultaneous downloads. It would also
keep track of the active connections and thus know when the last
completes, so it can trigger some final action.
Regards,
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