[英]AFNetworking crashes , downloading large files 200MB+
下載250MB以下的文件沒有問題,但是當我嘗試下載250MB + zip時,應用程序只是在未發出錯誤的情況下突然崩潰。 有時它會通過,有時它不會。 泄漏證實沒有臃腫或記憶喪失。
我現在也注意到崩潰只發生在使用xcode進行調試時。 當我運行應用程序並下載較大的文件時,我沒有調試沒有問題
下載更大的文件時,是否需要以不同方式處理AFNetworking類?
這是我的代碼
NSURL* url=[BFAppGlobals getServerURL:[M.Properties objectForKey:@"zip_path" ]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval:3600];
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
NSString* writePath=[BFAppGlobals getContentResourcePathForFile:strFile];
[delegate onStartDownload:M];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:writePath append:YES];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
int b=totalBytesRead ;
int total=totalBytesExpectedToRead;
float perc=(float)b/(float)total;
M.progress=perc;
[((NSObject*)delegate) performSelectorOnMainThread:@selector(onDataReceviedFromRequest:) withObject:M waitUntilDone:YES];
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* params=[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:writePath,writeDirectory, M, nil] forKeys:[NSArray arrayWithObjects:@"Path",@"Dir",@"Model", nil]];
[self performSelectorInBackground:@selector(unzipDownloaded:) withObject:params];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"fail! %@", [error localizedDescription]);
[delegate onErrorDownload:M WithError:[error localizedDescription]];
ActiveModel=nil;
}];
[operation start];
**************************** UPDATE ADDED CRASH LOG ***************************************
Thread 0 Crashed:
0 libobjc.A.dylib 0x37d24fbc objc_msgSend + 16
1 Foundation 0x35502508 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_0 + 12
2 CoreFoundation 0x381aa570 ___CFXNotificationPost_block_invoke_0 + 64
3 CoreFoundation 0x381360c8 _CFXNotificationPost + 1400
4 Foundation 0x354763f4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 60
5 Foundation 0x35477c24 -[NSNotificationCenter postNotificationName:object:] + 24
6 BFiPad 0x0006d2fc 0x1000 + 443132
7 CoreFoundation 0x3813d224 -[NSObject performSelector:withObject:] + 36
8 Foundation 0x35517750 __NSThreadPerformPerform + 344
9 CoreFoundation 0x381b2afc __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 8
10 CoreFoundation 0x381b22c8 __CFRunLoopDoSources0 + 208
11 CoreFoundation 0x381b106e __CFRunLoopRun + 646
12 CoreFoundation 0x381344d6 CFRunLoopRunSpecific + 294
13 CoreFoundation 0x3813439e CFRunLoopRunInMode + 98
14 GraphicsServices 0x37f0bfc6 GSEventRunModal + 150
15 UIKit 0x31cb473c UIApplicationMain + 1084
16 BFiPad 0x00003f72 0x1000 + 12146
17 BFiPad 0x00003f30 0x1000 + 12080
似乎這只發生在調試模式下。 用完調試模式或發布更多解決了這個問題
它看起來並不像特定的任何operation
,所以不能保證它會留在內存中。 嘗試使用NSOperationQueue
(可能是一個附加到AFHTTPClient
,它會清理大量代碼),或者在啟動上傳的控制器中將其設置為保留屬性。
我知道這個問題已經過時但問題似乎仍然存在,所以無論如何我都會發布我的解決方案。
我有同樣的問題,它在下載大約250MB后崩潰,並發現它是由每次下載幾個字節時調用下載進度塊這意味着它被稱為A LOT。 每秒很多次。 因此,當從這里調用委托方法(可能會做一些事情)時,它會變得非常耗費內存。
我的解決方案是跟蹤下載進度,如果更改很重要,我只調用委托方法(我決定超過1%):
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = ((float)totalBytesRead) / totalBytesExpectedToRead;
if ((progress - totalProgress) > 0.01) {
totalProgress = progress;
[delegate updateProgress:progress];
}
}];
float totalProgress
是一個實例變量。
此修復程序將內存使用量從崩潰時的大約280MB減少到我的應用程序已經使用的大約30MB。 即在下載過程中沒有明顯的內存上升(假設您直接下載到文件,而不是內存)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.