如何使用AFNetworking Library下载大量数据以在ios中显示更好的性能? [英] How to download the large amount of data using AFNetworking Library to show the better performance in ios?

查看:90
本文介绍了如何使用AFNetworking Library下载大量数据以在ios中显示更好的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using AFNetworking Library, How to download the large amount of data using AFNetworking Library to show the better performance?

我已执行以下步骤:

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

".zip" files downloading the content from server.

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

I have added one url , like this i have so many url's to download the content from the server. It takes too much time. How to perform the fast downloading from server.

我的查询是:如果在服务器上未找到该URL,则它将执行失败状态。
,但 .zip存储在documents文件夹中,内容为零字节之类的空内容。

My Query is: If the url was not found on server, then it executes failure state. but the ".zip" is storing in documents folder with empty content like zerobytes.

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

while am trying to extract the .zip it shows .cpgz format. Let us know how to resolve this?

-(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。请让我们知道,现在我不想下载文档文件夹路径中存在的文件,也让我们知道文件是否已成功下载。

1.Please let us know, now i dont want to download the which is exist in documents folder path and also let us know whether the the file has been downloaded sucessfully or not.

推荐答案

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

You need to take care of below points to achieve your functionality


  1. 在下载之前检查文件是否存在

  2. 实施故障阻止并在您的 activityPath

  1. Check file is already exist or not before download
  2. Implement failure block and remove file at your activityPath

参考,请检查以下代码段。

For reference, please check below code snippet.

-(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);
}];

}

这篇关于如何使用AFNetworking Library下载大量数据以在ios中显示更好的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆