Re: NSURLDownload in an action?
Re: NSURLDownload in an action?
- Subject: Re: NSURLDownload in an action?
- From: Robert Nicholson <email@hidden>
- Date: Thu, 31 May 2007 20:34:18 +0700
A couple of observations...
If I put the following at the end of my runJob method that I want to
block until all async downloads have finished the delegate methods fire
while (completedCount < [batch count]) {
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate
dateWithTimeIntervalSinceNow:0.1]];
}
Is this legal? Am I going to get bitten later by doing this
For now this simple implementation just increments completedCount in
- (void)downloadDidFinish:(NSURLDownload *)download
{
?
Secondly,
When you use NSURLDownload you have to rely on a delegate method
being called in order to get the path that was used to save the file.
In that delegate method you try to maintain a map of download to
path so you can easily lookup the path for a download instance
later. Turns out NSURLDownload doesn't implement copyWithZone: and so
cannot by used as the key in an NSDictionary.
Is that typical?
On May 31, 2007, at 5:34 PM, Robert Nicholson wrote:
Forgive the cross posting it's probably more relevant to general
cocoa than automator despite my attempts to use it in an automator
action. the key point being automator actions have to be
synchronous ie. all work is done before the top method returns.
I'm trying to use NSURLDownload in an action but given it's
asychronous nature I need the method to block until the download is
fully complete.
How do you do that? if I do using an NSConditionLock doesn't that
block the same thread that's receiving the delegate method calls?
ie. I'd set the condition in downloadDidfinish but if I do
[batchLock lockWhenCondition:0];
[batchLock unlock];
to block this method from returning (this is a child thread of
main) won't that then
stop any delegate methods from being fired?
Currently no delegate methods are being called after
for (i=0;i<[batch count];i++) {
NSURL *url = [batch objectAtIndex:i];
if ([[url scheme] isEqualToString:@"http"] || [[url scheme]
isEqualToString:@"https"]) {
NSURLRequest *r = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
NSURLDownload *d = [[NSURLDownload alloc] initWithRequest:r
delegate:self];
NSString *destination = [@"/tmp/" stringByAppendingString:[[url
path] lastPathComponent]];
[d setDestination:destination allowOverwrite:YES];
[downloads addObject:d];
[d release];
NSLog(@"%@",url);
}
}
[batchLock lockWhenCondition:0];
[batchLock unlock];
The idea being that ticks the condition down until it's 0 which
represents all downloads have completed.
- (void)downloadDidFinish:(NSURLDownload *)download
{
[batchLock lock];
[batchLock unlockWithCondition:[batchLock condition] - 1];
NSLog(@"Thread %p did finish %@", [NSThread currentThread],
[[download request] URL]);
}
How can you allow NSURLDownload to finish the download before
returning but yet still allow for it's delegate methods to fire?
this method should return an array of the paths for the downloaded
files
NSEnumerator *de = [downloads objectEnumerator];
id download = nil;
while (download = [de nextObject]) {
NSString *path = [pathDict objectForKey:download];
if (path) {
[results addObject:path];
}
}
return results;
I build the dictionary
- (void)download:(NSURLDownload *)download didCreateDestination:
(NSString *)path
{
[pathDict setObject:path forKey:download];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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