Oli*_*ite 5 cocoa objective-c autorelease nsurlcache
CSURLCache旨在缓存资源以进行脱机浏览,因为NSURLCache只将数据存储在内存中.
如果cachedResponse在返回应用程序崩溃之前自动释放,如果没有,则只是泄漏对象.
任何可以照到这上面的光都会非常感激.
请注意stringByEncodingURLEntities是一种类别方法NSString.
@interface CSURLCache : NSURLCache {} @end
@implementation CSURLCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[[[request URL] absoluteString] stringByEncodingURLEntities]];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[request URL]
MIMEType:nil
expectedContentLength:[data length]
textEncodingName:nil];
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response
data:data];
[response release];
[data release];
return cachedResponse;
}
return nil;
}
@end
Run Code Online (Sandbox Code Playgroud)
更新:向Apple提交雷达后,似乎这是一个已知问题(雷达#7640470).
\n\n\nRun Code Online (Sandbox Code Playgroud)\n- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request\n
嗯,这不是alloc、new或copy方法\xe2\x80\xa6
\xe2\x80\xa6 和 CSURLCache 不会在任何地方保留该对象,因此它不拥有它。
\n\n所以,你需要自动释放它。
\n\n当然,这意味着除非有东西保留它,否则该物体注定会失败。您的应用程序崩溃了,因为它在对象死亡后尝试使用该对象。
\n\n使用 Zombies 模板在 Instruments 下运行您的应用程序。查看应用程序崩溃的位置以及cachedResponseForRequest:调用时它正在执行的操作。调用者需要拥有该对象,直到应用程序崩溃为止,然后释放它。
| 归档时间: |
|
| 查看次数: |
2497 次 |
| 最近记录: |