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

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

我遵循以下步骤:

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

我已经添加了一个url,就像我有这么多url从服务器下载内容。 这需要太多的时间。 如何从服务器执行快速下载。

我的查询是:如果在服务器上找不到url,则执行失败状态。 但是“.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); }]; } 

请让我们知道,现在我不想下载文件夹path中存在的文件,也让我们知道文件是否已经下载成功。

一些想法:

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

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

  3. 如果你不想下载你的Documents文件夹中已经存在的文件,那么你必须自己NSFileManager这个逻辑(例如,使用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]; }); 

您需要注意以下几点来实现您的function

  1. 检查文件已经存在或没有下载
  2. 在你的activityPath实现失败块并删除文件

作为参考,请查看下面的代码片段。

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