ios – 即使缓存策略设置为ReloadIgnoringLocalAndRemoteCacheData,Alamofire从缓存加载

我设置缓存策略以请求在Alamofire忽略本地缓存.

然后我加载一个带有网络连接的视图控制器,然后我断开网络连接,杀死应用程序并再次运行.

现在没有网络可用错误没有显示(即alamofire不创建nserror对象)创建,而应用程序运行,好像请求成功从缓存中获取数据显而易见.奇怪的是,当我试图检查缓存的数据使用

NSURLCache.sharedURLCache().cachedResponseForRequest(request)

数据是从缓存返回的,否则返回

我可以防止缓存响应的唯一方法是执行NSURLCache.sharedURLCache().removeAllCachedResponses()

let request = NSURLRequest(URL: NSURL(string: url)!,cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData,timeoutInterval: 100)

    Alamofire.manager.request(method,request,parameters:params)
    .responseJSON { (request,response,data,error) in
        if let anError = error {
            if anError.code == NSURLErrorNotConnectedToInternet {
                UIAlertView(title: "Alert",message: "No Network Connection Available",delegate: nil,cancelButtonTitle: "ok").show()
            }    

        } else if let data: AnyObject = data {
            println(NSURLCache.sharedURLCache().cachedResponseForRequest(request))
 //prints nil

    }

} 
}

我想要做的是仅在网络连接不可用的情况下从缓存加载数据,类似于有限的离线模式.如何做?

解决方法

我在一个项目中使用这种方式,它的工作:
let mutableuRLRequest = NSMutableuRLRequest(URL: SERVICEURL)
mutableuRLRequest.HTTPMethod = "POST"

mutableuRLRequest.HTTPBody = self.createJson()
mutableuRLRequest.setValue("application/json",forHTTPHeaderField: "Content-Type")
mutableuRLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

request(mutableuRLRequest).validate().responseJSON{ response in...

希望它有帮助.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...