原理:先给NSURLSession地Configuration设置一个内存和本地代理,原来的网络请求结束后会查找缓存的代理字典,并执行代理对象对应的操作方法,需要做的就是拦截错误的方法,返回缓存的数据

AFURLSessionManager.m

- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super init];
if (!self) {
return nil;
} if (!configuration) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
} self.sessionConfiguration = configuration; #pragma mark 在这里给网络加上一个缓存
[self addURLCacheForRequestSession:configuration]; self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = ; self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue]; self.responseSerializer = [AFJSONResponseSerializer serializer]; self.securityPolicy = [AFSecurityPolicy defaultPolicy]; #if !TARGET_OS_WATCH
self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
#endif self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init]; self.lock = [[NSLock alloc] init];
self.lock.name = AFURLSessionManagerLockName; [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDataTask *task in dataTasks) {
[self addDelegateForDataTask:task uploadProgress:nil downloadProgress:nil completionHandler:nil];
} for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
[self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
} for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
}
}]; return self;
} #pragma mark 添加缓存
-(void)addURLCacheForRequestSession:(NSURLSessionConfiguration*)configuration
{
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.2" options:NSNumericSearch] == NSOrderedAscending) {
configuration.URLCache = [NSURLCache sharedURLCache];
}
else {
configuration.URLCache = [[NSURLCache alloc] initWithMemoryCapacity: * *
diskCapacity: * *
diskPath:@"customer_request_cache"];
}
}

AFURLSessionManagerTaskDelegate  实现方法添加部分代码

#pragma mark - NSURLSessionTaskDelegate

- (void)URLSession:(__unused NSURLSession *)session
task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error
{
__strong AFURLSessionManager *manager = self.manager; __block id responseObject = nil; __block NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
userInfo[AFNetworkingTaskDidCompleteResponseSerializerKey] = manager.responseSerializer; //Performance Improvement from #2672
NSData *data = nil;
if (self.mutableData) {
data = [self.mutableData copy];
//We no longer need the reference, so nil it out to gain back some memory.
self.mutableData = nil;
} if (self.downloadFileURL) {
userInfo[AFNetworkingTaskDidCompleteAssetPathKey] = self.downloadFileURL;
} else if (data) {
userInfo[AFNetworkingTaskDidCompleteResponseDataKey] = data;
} if (error) {
#pragma mark 网络错误时读取网络缓存数据
if (error.code == -) {
NSURLCache *cache = self.manager.session.configuration.URLCache;
if (cache) {
NSCachedURLResponse *response = [cache cachedResponseForRequest:task.currentRequest];
dispatch_async(url_session_manager_processing_queue(), ^{
NSError *serializationError = nil;
responseObject = [manager.responseSerializer responseObjectForResponse:response.response data:response.data error:&serializationError]; if (self.downloadFileURL) {
responseObject = self.downloadFileURL;
} if (responseObject) {
userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
} if (serializationError) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
} dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(response.response, responseObject, serializationError);
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
});
}
}
else {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = error; dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, error);
} dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
} } else {
dispatch_async(url_session_manager_processing_queue(), ^{
NSError *serializationError = nil;
responseObject = [manager.responseSerializer responseObjectForResponse:task.response data:data error:&serializationError]; if (self.downloadFileURL) {
responseObject = self.downloadFileURL;
} if (responseObject) {
userInfo[AFNetworkingTaskDidCompleteSerializedResponseKey] = responseObject;
} if (serializationError) {
userInfo[AFNetworkingTaskDidCompleteErrorKey] = serializationError;
} dispatch_group_async(manager.completionGroup ?: url_session_manager_completion_group(), manager.completionQueue ?: dispatch_get_main_queue(), ^{
if (self.completionHandler) {
self.completionHandler(task.response, responseObject, serializationError);
} dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingTaskDidCompleteNotification object:task userInfo:userInfo];
});
});
});
}
}
//网络请求错误代码
enum {
NSURLErrorUnknown = -,
NSURLErrorCancelled = -,
NSURLErrorBadURL = -,
NSURLErrorTimedOut = -,
NSURLErrorUnsupportedURL = -,
NSURLErrorCannotFindHost = -,
NSURLErrorCannotConnectToHost = -,
NSURLErrorDataLengthExceedsMaximum = -,
NSURLErrorNetworkConnectionLost = -,
NSURLErrorDNSLookupFailed = -,
NSURLErrorHTTPTooManyRedirects = -,
NSURLErrorResourceUnavailable = -,
NSURLErrorNotConnectedToInternet = -,
NSURLErrorRedirectToNonExistentLocation = -,
NSURLErrorBadServerResponse = -,
NSURLErrorUserCancelledAuthentication = -,
NSURLErrorUserAuthenticationRequired = -,
NSURLErrorZeroByteResource = -,
NSURLErrorCannotDecodeRawData = -,
NSURLErrorCannotDecodeContentData = -,
NSURLErrorCannotParseResponse = -,
NSURLErrorInternationalRoamingOff = -,
NSURLErrorCallIsActive = -,
NSURLErrorDataNotAllowed = -,
NSURLErrorRequestBodyStreamExhausted = -,
NSURLErrorFileDoesNotExist = -,
NSURLErrorFileIsDirectory = -,
NSURLErrorNoPermissionsToReadFile = -,
NSURLErrorSecureConnectionFailed = -,
NSURLErrorServerCertificateHasBadDate = -,
NSURLErrorServerCertificateUntrusted = -,
NSURLErrorServerCertificateHasUnknownRoot = -,
NSURLErrorServerCertificateNotYetValid = -,
NSURLErrorClientCertificateRejected = -,
NSURLErrorClientCertificateRequired = -,
NSURLErrorCannotLoadFromNetwork = -,
NSURLErrorCannotCreateFile = -,
NSURLErrorCannotOpenFile = -,
NSURLErrorCannotCloseFile = -,
NSURLErrorCannotWriteToFile = -,
NSURLErrorCannotRemoveFile = -,
NSURLErrorCannotMoveFile = -,
NSURLErrorDownloadDecodingFailedMidStream = -,
NSURLErrorDownloadDecodingFailedToComplete = -
}

给AFNetworking添加请求缓存功能实现在没有网络的情况下返回缓存数据的更多相关文章

  1. 微信小程序POST请求参数传递不到后台, 前台获取不到后端返回的数据, 以及 post 请求返回 404 但后台能收到数据

    1 微信小程序POST请求参数传递不到后台 需要在微信请求 wx.request 改变默认 header 配置为如下 wx.request({ url: 'test.php', //仅为示例,并非真实 ...

  2. Okhttp设置http缓存,在没有网络的情况下加载http缓存里面的内容

    HTTP_CACHE_FILENAME为缓存地址根路径: private final String HTTP_CACHE_FILENAME = "HttpCache"; priva ...

  3. localstorage实现带过期时间的缓存功能

    前言 一般可以使用cookie,localstorage,sessionStorage来实现浏览器端的数据缓存,减少对服务器的请求. 1.cookie数据存放在本地硬盘中,只要在过期时间之前,都是有效 ...

  4. (转)全面认识一下.NET 4的缓存功能

    很多关于.NET 4.0新特性的介绍,缓存功能的增强肯定是不会被忽略的一个重要亮点.在很多文档中都会介绍到在.NET 4.0中,缓存功能的增强主要是在扩展性方面做了改进,改变了原来只能利用内存进行缓存 ...

  5. iOS UIWebview添加请求头的两种方式

    1.在UIWebviewDelegate的方法中拦截request,设置request的请求头,废话不多说看代码: - (BOOL)webView:(UIWebView *)webView shoul ...

  6. 【Java/Android性能优5】 Android ImageCache图片缓存,使用简单,支持预取,支持多种缓存算法,支持不同网络类型,扩展性强

    本文转自:http://www.trinea.cn/android/android-imagecache/ 主要介绍一个支持图片自动预取.支持多种缓存算法.支持二级缓存.支持数据保存和恢复的图片缓存的 ...

  7. 【高并发】高并发环境下构建缓存服务需要注意哪些问题?我和阿里P9聊了很久!

    写在前面 周末,跟阿里的一个朋友(去年晋升为P9了)聊了很久,聊的内容几乎全是技术,当然了,两个技术男聊得最多的话题当然就是技术了.从基础到架构,从算法到AI,无所不谈.中间又穿插着不少天马行空的想象 ...

  8. Volley网络框架完全解析(缓存篇)

    在上一篇中讲完了Volley框架怎么使用,那么这篇就来讲讲Volley框架的缓存机制 我们看Volley内部源码发现: Volley框架内部自己处理了DiskBasedCache硬盘缓存,但是没有处理 ...

  9. 防抖与节流 & 若每个请求必须发送,如何平滑地获取最后一个接口返回的数据

    博客地址:https://ainyi.com/79 日常浏览网页中,在进行窗口的 resize.scroll 或者重复点击某按钮发送请求,此时事件处理函数或者接口调用的频率若无限制,则会加重浏览器的负 ...

随机推荐

  1. s:iterator 标签使用错误记录

    <s:iterator value="newMarriageMoveList" id='tpNewMarriage' status="number"> ...

  2. ASP.NET CORE下用盛派微信SDK取微信openid

    用CORE做项目用到微信的相关东西,听说那个盛派微信SDK很火,自己弄了下,只是简单的用用,用户访问页面取微信openid

  3. [Big Data - Kafka] kafka学习笔记:知识点整理

    一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 2.冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险. ...

  4. github新建repositories后import已有code 随后同步更新

    github上,可以forks别人已有的项目,而且同步更新合并也很方便,但如果是自己新建的项目,而导入的是别人的代码修改后,别人的又更新了,自己想获取他的更新,怎么办呢?其实很简单. # git cl ...

  5. IntelliJ IDEA 中文乱码配置

    总共有下面几种乱码的解决方案: 工程乱码 执行main函数时,控制台乱码 运行tomcat时,控制台乱码 PS: 如果下面方案不生效时,打开IDEA安装目录找到 idea.exe.vmoptions( ...

  6. Java如何验证电子邮件地址格式?

    在Java编程中,如何验证电子邮件地址格式? 以下示例演示如何使用String类的matches()方法来验证电子邮件地址. package com.yiibai; public class Vali ...

  7. 正确释放WORD对象(COM组件) COMException: 被调用的对象已与其客户端断开连接

    本来form method=post本页面 修改为其他页面 action=save.aspx后没问题 其他问题可参考以下: 引自:http://topic.csdn.net/u/20090108/17 ...

  8. 多线程开发之二 NSOperation

    效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UITableViewControll ...

  9. 揭秘IT人才特点:中美印日四国程序员比较

    不知道大家是否已经看过这篇文章,感觉比较客观.   来自:http://www.programmer.com.cn/282/        最近以裁判的身份参加了公司举办的编程大赛,发现高手云集,对公 ...

  10. VS2017 配置freeglut3.0.0

    配置freeglut: 1. 先下载cmake和freeglut3.0.0文件,并使用cmake编译freeglut,再使用vs2017生成解决方案,详细步骤见https://blog.csdn.ne ...