AFNetworking - do not cache response

前端 未结 8 1491
说谎
说谎 2020-12-08 16:51

I\'m using this code to pull a simple JSON feed from a server:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.         


        
相关标签:
8条回答
  • 2020-12-08 17:11

    With AFNetworking 3:

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfiguration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL
                                                             sessionConfiguration:sessionConfiguration];
    
    0 讨论(0)
  • 2020-12-08 17:12

    Just do:

    manager.requestSerializer.cachePolicy = NSURLRequestCachePolicyReturnCacheDataElseLoad
    
    0 讨论(0)
  • 2020-12-08 17:17

    What you are experiencing is the effect of the URL cache (see NSURLCache).

    The caching behavior of the request can be defined by setting a "Cache Policy" for the NSMutableURLRequest object, e.g.:

    NSMutableURLRequest* request = ...;
    [request setCachePolicy: myCachePolicy];
    

    The default caching behavior (NSURLRequestUseProtocolCachePolicy) is appropriate for the current protocol, which is HTTP. And for the HTTP protocol, a GET requests will be cached by default!

    And, AFNetworking does not change the default behavior of the request!

    Now, you could set another cache policy, for example:

    NSURLRequestReloadIgnoringLocalCacheData

    Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

    This is likely the desired behavior you want to achieve:

    [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
    

    The problem here is, that the super "convenient" API does not provide a way to configure the URL cache behavior of the request. You cannot access the used request at all.

    Thus, I would suggest to use a lower level API where you have control about the created NSMutableURLRequest object, and set the cache policy accordingly.

    0 讨论(0)
  • 2020-12-08 17:21
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:@"no-store" forHTTPHeaderField:@"Cache-Control"];
    [manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    

    Adding the Cache-Control:no-store header to the request, assuming your server is implemented correctly, will return a response with the same header thus disabling NSURLCache disk cache for any request that contains this header.

    0 讨论(0)
  • 2020-12-08 17:30

    For Swift poeple

    let manager = AFHTTPSessionManager()
    manager.requestSerializer.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
    
    0 讨论(0)
  • 2020-12-08 17:35

    try to add some rubbish at the end of your URL (for example, timestamp)

    kDataUrl = [NSString stringWithFormat:@"%@?%f", kDataUrl, [NSDate timeIntervalSinceReferenceDate]];
    

    In this case, you would request fresh data every time. That works for me))

    0 讨论(0)
提交回复
热议问题