Alamofire缓存数据篇mark
2018-01-22 本文已影响33人
lotawei
许多app需要在无网络的情况下缓存数据,做法记录一下
1.创建特定的config sessionmanager
let netmanager: SessionManager = {
let configuration = URLSessionConfiguration.default
let reachability = NetworkReachabilityManager()
if (reachability?.isReachable)! {
configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
}else
{
configuration.requestCachePolicy = .returnCacheDataElseLoad
}
return Alamofire.SessionManager(configuration: configuration)
}()
2.appdelegate中开启缓存
NetworkReachabilityManager.init()?.startListening()
let urlCache = URLCache.init(memoryCapacity: 4*1024*1024, diskCapacity: 20*1024*1024, diskPath: nil)
URLCache.shared = urlCache
3.使用 alamofire.request 统一换成netmanager.request 即可