Hello All.
I'm trying to download many URLs. However, I'm having some trouble. I can download a single URL but if I try to "harvest" all urls out of that page and then download all of those, I never receive a CALLBACK for those called from my scanResults method below.
I receive a callback from the first (i.e. main URL) and the page is downloaded fine. I pass the filename to a seperate thread (scanResults) which opens the file and tries to download every url in that page. Again, I don't get a callback. Any ideas why??? THANKS.
- (void)downloadDidFinish:(NSURLDownload *)download { NSEnumerator *e; id object;
NSLog(@"download did finish: %@ ************************************************",download);
e=[urlArray objectEnumerator]; while ( object = [e nextObject] ) { if([object objectForKey:@"nsurldownload"] == download) { float difference = [[NSDate date] timeIntervalSinceReferenceDate] - [[object objectForKey:@"startTime"] timeIntervalSinceReferenceDate]; [object setObject:[NSNumber numberWithFloat:difference] forKey:@"latency"]; [downloadHistoryTV reloadData];
if([harvestSwitch state] == NSOnState) { if(currentPageCount < totalPagesPermitted) [NSThread detachNewThreadSelector:@selector(scanResults:) toTarget:self withObject:[object objectForKey:@"filename"]]; } } } // release the connection [download release];
// do something with the data // NSLog(@"%@",@"downloadDidFinish"); }
However, within the thread call to scanResults, I parse each http:// address and create a new NSURLDownload. However, they never begin or finish. By this, I mean they never get a callback. I've done this within the same main thread to see if somehow threading was causing the problem, but it acts the same.
-(IBAction)scanResults:(id)sender { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *aRow; NSString *fileContents; NSArray *rowArray; NSArray *rowComponents; NSString *pageFile;
// Now load the results fileContents = [NSString stringWithContentsOfFile:sender]; rowArray = [fileContents componentsSeparatedByString:@"\n"]; int i,rowCount = [rowArray count]; for(i=0;i<rowCount;i++) { aRow = [rowArray objectAtIndex:i]; rowComponents = [aRow componentsSeparatedByString:@"http://"]; if([rowComponents count] < 2) continue;
NSString *tmpString = [NSString stringWithString:@"\""]; NSArray *parseArray = [[rowComponents objectAtIndex:1] componentsSeparatedByString:tmpString];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:[parseArray objectAtIndex:0]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
// create the connection with the request // and start loading the data NSURLDownload *theDownload=[[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; if (theDownload) { // set the destination file now pageFile = [[NSString alloc] initWithFormat:@"%@/%f",ironWebDir,[[NSDate date] timeIntervalSinceReferenceDate]]; [theDownload setDestination:pageFile allowOverwrite:YES]; NSMutableDictionary *record = [NSMutableDictionary dictionary]; [record setObject:theDownload forKey:@"nsurldownload"]; [record setObject:[parseArray objectAtIndex:0] forKey:@"url"]; [record setObject:[NSDate date] forKey:@"startTime"]; [record setObject:pageFile forKey:@"filename"];
[urlArray addObject:record]; [downloadHistoryTV reloadData]; }
} [pool release]; }
|