Oleg
Oleg

Reputation: 3014

iPhone - How to download big amount of files

I need to download a number of files from the server. What is the best way to do it? All documents are stored in NSMutableArray and for each documents there are two files - the document itself and its change log. So what I do is:

- (void)downloadDocuments:(int)docNumber
{
    NSString *urlString;
    NSURL *url;   
    for (int i=0; i<[items count]; i++) {
        [progressBar setProgress:((float)i/[items count]) animated:YES];
        urlString = [[items objectAtIndex:i] docUrl];
        url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
        [self downloadSingleDocument:url];
        urlString = [[items objectAtIndex:i] changeLogUrl];
        url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
        [self downloadSingleDocument:url];
    }
    urlString = nil;
    url = nil;
    [self dismissModalViewControllerAnimated:YES];
}

- (void)downloadSingleDocument:(NSURL *)url
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    [req addValue:@"Basic XXXXXXX=" forHTTPHeaderField:@"Authorization"];
    downloadConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
{
    if (conn == downloadConnection) {
        NSString *filename = [[conn.originalRequest.URL absoluteString] lastPathComponent];
        filename = [filename stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        filePath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:filename];
        [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];

        file = [[NSFileHandle fileHandleForUpdatingAtPath:filePath] retain];
        if (file)
        {
            [file seekToEndOfFile];
        }
    }

}


- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
{
    if (conn == downloadConnection) {
        if (file) { 
            [file seekToEndOfFile];
        }
        [file writeData:data];
    }

}


- (void)connectionDidFinishLoading:(NSURLConnection *)conn
{

    if (conn==downloadConnection) {
        [file closeFile];
    }
}

And my problem is that only the last file is downloaded. Any suggestions on what I am doing wrong? Thanks in advance for help!

Upvotes: 0

Views: 356

Answers (1)

TRD
TRD

Reputation: 1027

The problem is that you "overwrite" the member var "downloadConnection" within your loop with a new instance of NSURLConnection (through method call downloadSingleDocument). Doing this leads to the case that the if-statements within your didReceiveResponse, didReceiveData and connectionDidFinish methods will only evaluate to true with the latest created connection. Try using a list of connections to avoid this.

Upvotes: 1

Related Questions