【发布时间】:2012-03-14 02:37:08
【问题描述】:
我正在开发一个下载资源并将其写入磁盘以供以后离线使用的应用程序,它始终是自定义内容。目前,我们正在处理大约 4000 个 JPG 的内容。用户初始化将内容下载到 iPad 上,并且 UI 中有一个进度条,因此用户基本上会等到完成。问题是分配了大约 180 - 190 MB 的内存,它崩溃了。
我在 Instruments 中看到的是 CFData (store) 是罪魁祸首,我的理解是 CFData (store) 是 NSURLConnection 请求的缓存。
我试过了:
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
和
[[NSURLCache sharedURLCache] removeAllCachedResponses];
以及设置缓存策略,没有任何改善。
作为参考,这是我的帖子请求的样子:
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[xmlMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postBody];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&httpError];
任何帮助都会得到热烈的掌声。
【问题讨论】:
-
您是否使用辅助线程来执行此操作?
-
另外,您是否大致了解您希望下载的总 MB 数?我想知道这与您提供的 190 MB 数字相比如何。
-
@Jim 不,不是辅助线程。当这些请求通过时,主线程上的进度条正在更新。就总大小而言,在我的模拟器中查看文档文件夹,大约 300 - 400 MB。
标签: objective-c ios nsurlconnection nsurlcache