0

我正在开发一个下载资源并将它们写入磁盘以供以后离线使用的应用程序,它始终是自定义内容。目前,我们正在处理大约 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];

任何帮助都会得到热烈的掌声。

4

1 回答 1

0

If you really want to do this with synchronous requests, then you might consider explicitly retianing and releasing the objects used in each request. Using the autorelease pool means that the leftover detritus from every request sits in the autorelease pool until the pool is drained.

If you have a for loop or some other loop handling these requests, you can also create a more local autorelease pool within the loop and then drain it just before the loop ends. If you do that and you have data that you want to retain beyond the scope of that loop (and the pool) you should retain it in the loop and relase it at some point later.

At some point, depending on your experience level, you should consider doing this all on a secondary thread. You can treat each fetch as a self-contained operation and then used something like NSOperation and NSOperationQueue to manage it. That way, you can launch multiple operations simulataneously and not block your thread while waiting for each response.

于 2012-02-23T18:23:51.890 回答