How To Disable AFNetworking Cache

后端 未结 6 2172
无人及你
无人及你 2020-12-02 15:54

Is it possible to disable all the cache features from AFNetworking?

I am building my own custom cache system and don\'t want this to take up disk space too.

相关标签:
6条回答
  • 2020-12-02 16:29

    Unfortunately non of these methods worked for me. The only way I've managed to get the cache disabled is this way:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    

    Here's the original answer

    0 讨论(0)
  • 2020-12-02 16:29

    This may depend on your server implementation, but adding Cache-Control:no-store header to your outgoing request should result in a response that contains the same header, thus commanding NSURLCache to not cache the response to disk.

    IMHO this is a better approach than disabling disk-caching completely, especially if you're writing SDK code that will be used by other applications which may want to utilize NSURLCache.

    0 讨论(0)
  • 2020-12-02 16:40

    Cacheing is handled application-wide by NSURLCache. If you don't set a shared cache, requests are not cached. Even with a shared NSURLCache, the default implementation on iOS does not support disk cacheing anyway.

    That said, unless you have a very particular reason to write your own cacheing system, I would strongly recommend against it. NSURLCache is good enough for 99.9% of applications: it handles cache directives from incoming responses and uses them appropriately with new requests, and does so automatically in a way that is unlikely to be a performance bottleneck in your application. As someone who has wasted untold hours making one myself (and later throwing it away since it was completely unnecessary), I'd say that there are much better places to focus your development attention.

    0 讨论(0)
  • 2020-12-02 16:44

    I've found on iOS 6 that requests are sometimes cached, even if the request has NSURLRequestReloadIgnoringCacheData. Adding a cache response block that returns nil prevents the request from being cached.

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                                                  cachePolicy:NSURLRequestReloadIgnoringCacheData
                                              timeoutInterval:20];
    AFJSONRequestOperation *op =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request];
    
    // DISABLE CACHE //
    [op setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
        return nil;
    }];
    
    [op start];
    
    0 讨论(0)
  • 2020-12-02 16:48

    Initially I found Jason Moore's answer to work, however recently I have noticed my app is still caching requests. I am not using the latest AFNetworking, so I do not know if caching has been addressed in more recent builds.

    Apple's URLCache Project has this to say:

    By default, the Cocoa URL loading system uses a small shared memory cache. We don't need this cache, so we set it to zero when the application launches.

    And then does this to disable the cache.

    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
                                                            diskCapacity:0
                                                                diskPath:nil];
    [NSURLCache setSharedURLCache:sharedCache];
    [sharedCache release];
    

    This will disable all caching for your whole app, which may not be ideal in some situations, but as NSURLRequest is not honouring the requested cache policy, it is the only option left that I can see.

    0 讨论(0)
  • 2020-12-02 16:51
    NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    
    0 讨论(0)
提交回复
热议问题