相关疑难解决方法(0)

防止NSURLSession缓存响应

为什么它会缓存响应.它返回先前获取的响应.它甚至可以在关闭网络连接时工作.重置iOS模拟器似乎也不起作用.提出请求,然后再次使用Internet脱机工作(缓存).

public let urlSession: NSURLSession

public init()
{
    // ...

    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    urlSession = NSURLSession(configuration: configuration)
}

func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
{
    let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
    let url = NSURL(string: urlString)
    let request = oauthInstance.request(forURL: url!)

    request.HTTPMethod = httpMethod
    self.customOAuth2Manager.setupRequest(request)

    for (key, value) in headers {
        request.setValue(value, forHTTPHeaderField:key)
    }

    if let body = body where body != "" {
        let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding) …
Run Code Online (Sandbox Code Playgroud)

caching nsurlcache nsurlsession nsurlsessionconfiguration swift

20
推荐指数
2
解决办法
1万
查看次数

如何使特定URL的iOS缓存无效?

使用NSURLSession默认缓存,如何使特定URL的缓存无效?

我注意到NSURLCacheremoveCachedResponseForRequest:方法,但这需要一个NSURLRequest对象,我没有原始请求.我是否需要在创建它们时存储它们,以便我可以将它们传回,removeCachedResponseForRequest:或者我可以创建一个具有相应URL的新URL,即使它没有相同的标题也将用作相应的URL.字段和其他属性作为原始?

caching nsurlcache ios nsurlsession

6
推荐指数
2
解决办法
5521
查看次数

使用storeCachedResponse存储在缓存中后,不会检索URLresponse

目标

我正在尝试将URLRequest中的数据/响应注入我的缓存中的另一个 URLRequest.

建立

这只是一个示例代码.它已准备好被转储到一个项目中.

我正在尝试做的是使用从我的landscapeURLString网络请求中检索到的响应+数据...存储到我的会话缓存中以获取我的lizardURLString请求.

import UIKit

class ViewController: UIViewController {

    lazy var defaultSession : URLSession = {
        let urlCache = URLCache(memoryCapacity: 500 * 1024 * 1024, diskCapacity: 500 * 1024 * 1024, diskPath: "something")
        let configuration = URLSessionConfiguration.default
        configuration.urlCache = urlCache
        let session = URLSession(configuration: configuration)

        return session
    }()
    lazy var downloadLizzardbutton : UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitle("download lizard image OFFLINE", for: .normal)
        btn.backgroundColor = .blue
        btn.addTarget(self, …
Run Code Online (Sandbox Code Playgroud)

caching http nsurlcache ios nsurlsession

5
推荐指数
1
解决办法
493
查看次数

NSURLCache的removeCachedResponseForRequest无效

我创建了一个NSURLCache子类,它强制在指定的时间内缓存响应.这很好用,缓存的项目按预期过期.但是,当尝试使用NSURLCache的removeCachedResponseForRequest:方法强制删除缓存的响应时,我遇到了问题.

我想要实现的是允许用户强制立即重新加载远程数据.为此,我在发出请求时传递"ignoreCache"标志.我正常构建我的NSURLRequest,但要求我的NSURLCache删除给定请求的任何先前缓存的响应.但是,这似乎没有任何影响,因为缓存的结果仍然存在并在执行请求时使用.

NSURLCache周围的文档相当稀少.NSURLCache标头声明传递给NSURLRequest removeCachedResponseForRequest:用作查找关联的NSCachedURLResponse对象的键,但是关于该比较的后勤的信息很少.NSURLCache类是否期望接收生成缓存响应的相同NSURLRequest实例,或者只是比较它所代表的NSURL?

希望有人可以指出我正确的方向.

var error: NSError? = nil
var URLRequest: NSMutableURLRequest = self.operationManager.requestSerializer.requestWithMethod("GET", URLString: NSURL(string: request.URL, relativeToURL: self.operationManager.baseURL).absoluteString, parameters: request.parameters, error: &error)
URLRequest.cachePolicy = NSURLRequestCachePolicy.ReturnCacheDataElseLoad

// We've been asked to ignore the cache, so remove our previously cached response
if ignoreCache == true {
    NSURLCache.sharedURLCache().removeCachedResponseForRequest(URLRequest)
}
Run Code Online (Sandbox Code Playgroud)

这是我的NSURLCache子类中的Swift代码供参考:

// MARK: - NSURLCache
override func cachedResponseForRequest(request: NSURLRequest!) -> NSCachedURLResponse! {
    var cachedResponse: NSCachedURLResponse? = super.cachedResponseForRequest(request)
    if(cachedResponse != nil && cachedResponse!.userInfo != nil) {
        var cacheDate: NSDate? = …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch caching nsurlrequest ios afnetworking

3
推荐指数
1
解决办法
1885
查看次数