iPhone-如何下载大量文件

iPhone-如何下载大量文件,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我需要从服务器下载一些文件。最好的方法是什么? 所有文档都存储在NSMutableArray中,每个文档都有两个文件—文档本身及其更改日志。所以我要做的是: - (void)downloadDocuments:(int)docNumber { NSString *urlString; NSURL *url; for (int i=0; i<[items count]; i++) { [progressBar setProgress:((floa

我需要从服务器下载一些文件。最好的方法是什么? 所有文档都存储在NSMutableArray中,每个文档都有两个文件—文档本身及其更改日志。所以我要做的是:

- (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];
    }
}
-(void)下载文档:(int)文档编号
{
NSString*urlString;
NSURL*url;

对于(int i=0;i问题在于,您在循环中使用NSURLConnection的新实例“覆盖”成员var“downloadConnection”(通过方法调用downloadSingleDocument)。这样做会导致您的didReceiveResponse、didReceiveData和ConnectiondFinish方法中的if语句仅在最新创建的连接中计算为true。请尝试使用连接列表来避免这种情况。

您可以写一个示例吗?或者您知道如何下载文件列表的更好方法吗,将“downloadConnection”设置为NSMutableArray,而不是NSURLConnection。在“downloadSingleDocument”-方法中,使用[downloadConnection addObject:theConnectionVar]将新创建的NSURLConnection实例添加到列表中。在NSURLConnection委托方法(didReceiveResponse等)中在downloadConnection数组中循环所有条目,并为每个条目检查该条目是否匹配作为该方法的param给定的连接变量。如果匹配,则这是匹配的连接,您可以像以前一样进行。奇怪,但它不起作用…我所做的是getFileConnection=[[NSURLConnection alloc]initWithRequest:req委托:自启动中间:YES];[downloadConnection addObject:getFileConnection];然后在委托方法中检查([downloadConnection containsObject:conn])是否也从未触发我的委托方法,仅针对列表中的最后一个文件