如何使用AFNetworking Library下载大量数据以显示ios中更好的性能?

时间:2015-03-12 13:52:58

标签: ios afnetworking-2

我正在使用 AFNetworking 库, 如何使用AFNetworking Library下载大量数据以显示更好的性能?

我已按照以下步骤操作:

"的的.zip "从服务器下载内容的文件。

我添加了一个网址,像这样我有很多网址从服务器下载内容。这需要太多时间。如何从服务器执行快速下载。

我的查询是:如果在服务器上找不到网址,则会执行失败状态。 但是" .zip"存储在文档文件夹中,其中包含零字节等空内容。

在尝试提取 .zip 时,它会显示 .cpgz 格式。 让我们知道如何解决这个问题?

-(void)downloadSingFile:(NSUInteger )activityAtIndex totalCount:(NSUInteger )lessonTotalCount{
    // http://118.102.131.158/IYG_wrapper/IYG_updated/IYG_G7_L03/IYG_UN_Mall.zip

    NSString *activityUrl = [filterObjects objectAtIndex:activityAtIndex];
    NSURL *zipFileAtUrl  = [NSURL URLWithString:[activityUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    BOOL success = [libraryObj createFolderWithName:rootName];
    if (success) {
        activityPath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:rootName] stringByAppendingPathComponent:[zipFileAtUrl lastPathComponent]];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:zipFileAtUrl];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:activityPath append:NO];

    [operation setCompletionBlock:^{
        sleep(1);
        NSLog(@"sucessfully downloaded file %d",activityIndx);
        [self extractSingleActivityAtindex:activityIndx];

    }];
    [operation start];

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float percentDone = ((float)(totalBytesRead) / (float)(totalBytesExpectedToRead));
        self.tableView.alpha = 0.7;
        progressView.hidden = NO;
        progressAlert.hidden = NO;
        progressAlert.labelText = @"Downloading..";
        [self updateProgress:percentDone];
        NSLog(@"progress float value is: %.2f",percentDone);
    }];

}

1.请告诉我们,现在我不想下载文件夹路径中存在的内容,也让我们知道该文件是否已经成功下载。

2 个答案:

答案 0 :(得分:0)

有几点想法:

  1. 如果您想知道它是否成功,而不是使用setCompletionBlock,则应使用setCompletionBlockWithSuccess:failure:

  2. 在相关主题中,我可能倾向于将文件下载到某个临时文件(在NSTemporaryDirectory()文件夹中),并且只有在成功下载后,才将其移动到Documents文件夹。您希望完全消除任何正在进行下载的可能性,从而导致文档中的文件。

  3. 如果您不想下载Documents文件夹中已存在的文件,则必须自己编写该逻辑(即使用NSFileManager查看文件是否存在,如果不存在,然后才启动下载)。

  4. 取消sleep电话。你永远不想sleep(尤其是主线程)。如果您真的想在一秒钟之后触发某些事情,请使用dispatch_after

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"sucessfully downloaded file %d",activityIndx);
        [self extractSingleActivityAtindex:activityIndx];
    });
    

答案 1 :(得分:0)

您需要注意以下几点才能实现您的功能

  1. 下载前检查文件是否已存在
  2. activityPath
  3. 上实施失败阻止并删除文件

    供参考,请查看以下代码段。

    -(void)downloadSingFile:(NSUInteger )activityAtIndex totalCount:(NSUInteger )lessonTotalCount{
    // http://118.102.131.158/IYG_wrapper/IYG_updated/IYG_G7_L03/IYG_UN_Mall.zip
    
    NSString *activityUrl = [filterObjects objectAtIndex:activityAtIndex];
    NSURL *zipFileAtUrl  = [NSURL URLWithString:[activityUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    BOOL success = [libraryObj createFolderWithName:rootName];
    if (success) {
        activityPath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:rootName] stringByAppendingPathComponent:[zipFileAtUrl lastPathComponent]];
    }
     if ([[NSFileManager defaultManager]fileExistsAtPath:activityPath]) {
         NSLog(@"File Already Exist");
         [self extractSingleActivityAtindex:activityIndx];
         return;
     }
    NSURLRequest *request = [NSURLRequest requestWithURL:zipFileAtUrl];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:activityPath append:NO];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        sleep(1);
        NSLog(@"sucessfully downloaded file %d",activityIndx);
        dispatch_async(dispatch_get_main_queue(), ^() {
           [self extractSingleActivityAtindex:activityIndx];
        });
    
    
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //    NSLog(@"ERR: %@", [error description]);
        [[NSFileManager defaultManager]removeItemAtPath:activityPath error:nil];
    
    }];
    
    [operation start];
    
    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float percentDone = ((float)(totalBytesRead) / (float)(totalBytesExpectedToRead));
        self.tableView.alpha = 0.7;
        progressView.hidden = NO;
        progressAlert.hidden = NO;
        progressAlert.labelText = @"Downloading..";
        [self updateProgress:percentDone];
        NSLog(@"progress float value is: %.2f",percentDone);
    }];
    
    }