Ios 使用AFNetworking下载多个文件时接收内存警告

Ios 使用AFNetworking下载多个文件时接收内存警告,ios,memory-management,afnetworking-2,Ios,Memory Management,Afnetworking 2,我正在从UIGridViewCells下载电影文件。我的代码是: NSMutableURLRequest* rq = [[APIClient sharedClient] requestWithMethod:@"GET" path:[[self item] downloadUrl] parameters:nil]; [rq setTimeoutInterval:5000]; _downloadOperation = [[AFHTTPRequestOperation alloc] i

我正在从UIGridViewCells下载电影文件。我的代码是:

NSMutableURLRequest* rq = [[APIClient sharedClient] requestWithMethod:@"GET" path:[[self item] downloadUrl] parameters:nil];
    [rq setTimeoutInterval:5000];
    _downloadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:rq] ;
    _downloadOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:[[self item] localUrl] append:NO];
    __weak typeof(self) weakSelf = self;
    [_downloadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", [weakSelf.item localUrl]);
        [Helper saveItemDownloaded:weakSelf.item.productId];
        weakSelf.isDownloading = NO;
        [weakSelf.progressOverlayView removeFromSuperview];
        [weakSelf setUserInteractionEnabled:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        [weakSelf.progressOverlayView removeFromSuperview];
        [weakSelf setUserInteractionEnabled:YES];
        weakSelf.isDownloading = NO;
        }];
    [_downloadOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        float progress = totalBytesRead / (float)totalBytesExpectedToRead;
        weakSelf.progressOverlayView.progress = progress;
    }];
    [[NSOperationQueue mainQueue] addOperation:_downloadOperation];
ItemCell中的属性为:

@property (nonatomic, retain) AFHTTPRequestOperation *downloadOperation;
在1-2次成功下载(20mb)后,我收到内存警告。每次下载时使用的内存都会增加,下载完成后不会减少

从文书:
对下载的每个文件使用@autorelease:

for(File* file in fileList)
{
    @autoreleasepool {
        [self downloadFile:file];
    } 
}
这将释放在您下载的单独文件之间分配的所有变量和数据


你也应该追踪那些内存泄漏。我在instruments屏幕截图中看到了一些内容。

我认为使用AFNetworking下载文件的首选方法是设置“outputStream”属性

根据AFN网络文件:

在请求完成之前用于写入接收到的数据的输出流

默认情况下,请求完成后,数据将累积到一个缓冲区中,该缓冲区存储在
responseData
中。设置
outputStream
时,数据不会累积到内部缓冲区中,因此,已完成请求的
responseData
属性将为
nil
。设置输出流后,将在网络线程runloop中调度输出流


我也遇到了同样的问题,通过使用“outputStream”解决了这个问题。

您是否在内部进行for循环?对于下载操作,是的。但我发送了一个播放电影的列表。当我跟踪内存泄漏时,我看不到任何与我的代码相关的泄漏。我更新了我的问题,请检查。