Pah*_*nev 14 caching afnetworking xcode6 ios8
简而言之,我刚刚更新到Xcode 6以检查我的应用程序在iOS 8上的工作方式.我注意到即使它应该也不会使用缓存.我正在使用AFNetworking设置cachePolicy,如下所示:
sessionManager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
Run Code Online (Sandbox Code Playgroud)
我仍然有一个iOS 7设备,我在其中测试相同的代码,它按预期工作.
有没有人有这方面的解决方案,还是我们需要等待Apple修复它?
Ric*_*hin 12
我几乎可以肯定iOS 8.0已经破解NSURLSession了缓存HTTP响应数据的能力.关于这个问题我和Apple开了一个雷达.
这是我写的一些示例代码来证明这一点:
NSURLSession *session = [NSURLSession sharedSession];
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024
diskCapacity:32 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURL *URL = [NSURL URLWithString:@"http://i.imgur.com/b5pyONe.jpg"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:5];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error occurred");
} else {
NSLog(@"Fetched resource!");
}
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
request = [NSURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReturnCacheDataDontLoad
timeoutInterval:5];
task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Something bad happened: %@", error);
} else {
NSLog(@"Fetched resource!");
}
}];
[task resume];
Run Code Online (Sandbox Code Playgroud)
即使创建自己的NSURLSession- NSURLSessionConfiguration有一个NSURLCache你自己创建的 - 也无法解决这个问题.现在,如果您需要严重缓存响应,则必须使用NSURLConnection.