2

更新:內存警告下載文件,AFNetworking

我寫了一個非常簡單的下載代碼:

NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://xxxx.s3.amazonaws.com/products/ipad/xxxx.mp4"]]; 
for(int i=0; i<4; i++){ 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,[NSString stringWithFormat:@"xxxx%d.mp4",i]]; 
    AFDownloadRequestOperation* operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:filePath shouldResume:YES]; 
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO]; 
    [operation setShouldOverwrite:YES]; 
    [operation setProgressiveDownloadProgressBlock:^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
     NSLog(@"%f", (totalBytesRead/(float)totalBytesExpected)); 
    }]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"finished"); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(error.description); 
    }]; 
    [[NSOperationQueue mainQueue] addOperation:operation]; 
} 

我寫在我的項目viewDidLoad中註釋掉所有其他代碼。內存使用仍然是相同的,增加:

我創建了一個新的項目,我在新的項目中寫道完全相同的代碼。和內存使用情況是:

enter image description here

這是很好的。但我不明白爲什麼它在真實項目中有所不同?

+1

你的收藏視圖中有多少個單元格?如果你有一堆細胞都在做這種手術,事情會變得很快。 – alexshafran

+1

10個細胞這樣做。我想要的是,當下載完成時,關閉輸出流並釋放內存。 – Burak

回答

1

這是尷尬的手動下載。我創建了一個新項目,並複製相同的代碼,並且它沒有內存警告。下載操作不影響內存。我不明白這個問題。也許是因爲項目設置。

-2

嘗試使用NSOperationQueue

Get here

+0

嗨。我已經在我的代碼中使用了[[NSOperationQueue mainQueue] addOperation:_downloadOperation]。你什麼意思? – Burak

5

我想你打開殭屍模式。 產品 - >計劃 - >編輯方案
取消選中opition [啓用殭屍對象]

enter image description here

+0

您將需要以答案的形式提供信息。不知道如何解析你在這裏寫的東西。 – Lizz

+0

我的意思是取消那個名爲enable Zombie Objects的選項 – snorlax

+0

你救了我的命:) – Sasho

0

同樣的事情發生在我身上。禁用殭屍對象爲我工作。