0

I'm having a little problem using NSURLDownload. Basically I'm downloading two different files using the following code twice with the variable names changed.

NSURLRequest *requestUserfile = [NSURLRequest requestWithURL:[NSURL URLWithString:requestAgent_url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSURLDownload *downloadUserFile = [[NSURLDownload alloc] initWithRequest:requestUserfile delegate:self];

NSString *uf_tpath = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"userfile"]; 

if (downloadUserFile) {
[downloadUserFile setDestination:uf_tpath allowOverwrite:YES];
} else {
NSLog("ERROR: Problem while downloading user file [1].");
exit(0);
}

But I've a problem, according to Apple class reference this code is asynchronous and it calls - (void)downloadDidFinish:(NSURLDownload *)download when the download is finished.

My program needs the two files downloaded to a folder before start doing any processing, so how can I know when the two of them are downloaded? that will be called when the first one is finished :S

My first idea was to place the second file download int the downloadDidFinish of the first one... but then I will run into an infinite loop and I'll have two methods with the same name :S

PS: I'm new to Obj-C and Xcode, I used to do this using signals and slots on QT but there I can just define two slots different slots and connect them on right time.

Thanks.

TCB13
  • 3,067
  • 2
  • 39
  • 68
  • 1
    Check [this answer](http://stackoverflow.com/questions/332276/managing-multiple-asynchronous-nsurlconnection-connections/332483#332483) – Youssef Sep 13 '11 at 14:58
  • thanks, but... since I'm not expert how should I use that on my code? thx – TCB13 Sep 13 '11 at 15:51
  • 1
    Uh, I wouldn't use `exit(0)` in your applications. It'll confuse the user and you'll get rejected by Apple. – sudo rm -rf Sep 14 '11 at 17:47
  • Thanks "sudo rm -rf", but I'm only using now as debug later I'll change the info in a window ;) "you'll get rejected by Apple." - I'm not pretending to have this in the App Store. – TCB13 Sep 14 '11 at 18:31

1 Answers1

0

I ended up applying a if statement on the (void)downloadDidFinish:(NSURLDownload *)download to evaluate witch file is already on the disk and then download the other.

Like

NSString *file1_path = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"file1"];
NSString *file2_path = [ [ [NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"file2"]; 
BOOL f1_is_downdl = [[NSFileManager defaultManager] fileExistsAtPath:file1_path];   
BOOL f2_is_downdl = [[NSFileManager defaultManager] fileExistsAtPath:file2_path];


if ( f1_is_downdl && !(f2_is_downdl) ) { // File 1 is downloaded and the second is not...

    [download release];

    // Now download the file 2!
    NSString *requestFile2_url = [NSString stringWithFormat:@"http://host.org/software/%@-ag", req_id];
    NSURLRequest *requestFile2 = [NSURLRequest requestWithURL:[NSURL URLWithString:requestFile2_url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    NSURLDownload *downloadFile2 = [[NSURLDownload alloc] initWithRequest:requestFile2 delegate:self];

    if (downloadFile2) {
        [downloadAgent setDestination:file2_path allowOverwrite:YES];
    } else {
        NSLog(@"ERROR: Problem while downloading user file [2].");
        exit(0);
    }

} else if (f1_is_downdl && f2_is_downdl) { // Everything is downloaded!

    // Do something else.

}

Well... I know that this is probably the worst way in the world to do this but... It's the way I figured it out.

Thanks for the info anyway, one day (when I rewrite this with more Obj-C knowage) it might me useful for me.

TCB13
  • 3,067
  • 2
  • 39
  • 68
  • Just add an `NSMutableSet` to your delegate class and add the `NSURLDownload`s to it when you start them and remove them when finished or failed. As long as `[mutableSet count] != 0` you have active downloads. Additional benefit of still having the `NSURLDownload` instances with all their info around upon completion, not just the download path and/or URL. – Regexident Aug 01 '12 at 22:02