2013-10-02 75 views
1

我正在从UIGridViewCells下载电影文件。我的代码是:使用AFNetworking下载多个文件时收到内存警告

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),我收到内存警告。每次下载都会增加内存使用量,下载完成后永远不会降低。

enter image description here

从仪器:每个文件 enter image description here

+1

你在做这一切吗? –

+0

对于下载操作,是的。但我发了一个列表来播放电影。 – Burak

回答

0

使用@autorelease下载:

for(File* file in fileList) 
{ 
    @autoreleasepool { 
     [self downloadFile:file]; 
    } 
} 

这将释放所有变量和单独的文件下载之间分配数据。

此外,你应该追查这些内存泄漏。我在仪器屏幕截图中看到一些可见的。

+0

当我跟踪内存泄漏时,我看不到任何与我的代码有关的内容。我更新了我的问题,请检查。 – Burak

2

我相信用AFNetworking下载文件的首选方法是设置“outputStream”属性。

根据AFNetworking文档:

,用于接收写请求之前完成数据的输出流。

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

我有同样的问题

,通过使用“的OutputStream”解决了这个问题。