NSURLDownload in an action?
NSURLDownload in an action?
- Subject: NSURLDownload in an action?
- From: Robert Nicholson <email@hidden>
- Date: Thu, 31 May 2007 17:34:52 +0700
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