Search code examples
iosfiledownloadrequestasihttprequest

Downloading multiple files with ASIHTTPRequest


I'm working on a project where I need to have 3 different .plist files downloaded for a server but I only manage to get one of them downloaded. Anyone know how to get all 3 of them downloaded? I'm using ASIHTTPRequest. Mycode:

- (void)downloadPlist {
NSLog(@"Download in progress...");


progressView.alpha = 1.0;


// Here we're downloading the .plist file from a server to the app's Documents Directory.
// Create file manager
fileManager = [NSFileManager defaultManager];

// Point to Document directory
documentsDirectory = [NSHomeDirectory()
                      stringByAppendingPathComponent:@"Documents"];


documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

filePath = [documentsDirectory stringByAppendingPathComponent:@"example1.plist"];
filePath = [documentsDirectory stringByAppendingPathComponent:@"example2.plist"];
filePath = [documentsDirectory stringByAppendingPathComponent:@"example3.plist"];




// files from server.
NSURL *url = [NSURL URLWithString:@"http://myWeb.com/example1.plist"];
NSURL *url1 = [NSURL URLWithString:@"http://myWeb.com/example2.plist"];
NSURL *url2 = [NSURL URLWithString:@"http://myWeb.com/example3.plist"];



request = [ASIHTTPRequest requestWithURL:url];
request = [ASIHTTPRequest requestWithURL:url1];
request = [ASIHTTPRequest requestWithURL:url2];
[request setShowAccurateProgress:YES];
[request setDownloadDestinationPath:filePath];
[request setDownloadProgressDelegate:progressView];
[request setDelegate:self];
[request startAsynchronous];

}

I think I'm on the right track but not sure... cheers!


Solution

  • You have used same variable names in several places to get three files. First variable is "filePath"

    Change the variables to separate names for e.g. like this,

    filePath1 = [documentsDirectory stringByAppendingPathComponent:@"example1.plist"];
    filePath2 = [documentsDirectory stringByAppendingPathComponent:@"example2.plist"];
    filePath3 = [documentsDirectory stringByAppendingPathComponent:@"example3.plist"];
    

    Same is with "request" variable??

    Make correction for e.g. like this,

    request1 = [ASIHTTPRequest requestWithURL:url];
    request2 = [ASIHTTPRequest requestWithURL:url1];
    request3 = [ASIHTTPRequest requestWithURL:url2];
    

    Or if you want to have only 1 request object then maybe try loop over all 3 urls and assign request one by one. Make sure you supply different filePaths in your loop

    [request setDownloadDestinationPath:filePath**{N}**];