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).
[self.managedObjectContext deleteObject:self];
Run Code Online (Sandbox Code Playgroud)
从概念上讲,执行这行代码是否存在问题?