Can I indicate how long the response should be cached in AFNetworking?

I am a late converter from ASIHTTPRequestto AFNetworking. AFN is far superior so far, but one feature that I missed from ASI is its ability to indicate how long a specific item should be cached using an enumeration ASICacheStoragePolicy. This allows you to specify that the item should be cached only for this session, permanently or completely absent.

Is there something similar for AFNetworking?

+4
source share
3 answers

The latest version of AFNetworking is based on two caching mechanisms:

  • NSCache
  • NSURLCache NSURLConnection

, , , .
2- .

+1

, :

AFHTTPRequestOperationManager *operation= [AFHTTPRequestOperationManager manager] ;
operation.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

:

enum
{
    NSURLRequestUseProtocolCachePolicy = 0,
    NSURLRequestReloadIgnoringLocalCacheData = 1,
    NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
    NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

    NSURLRequestReturnCacheDataElseLoad = 2,
    NSURLRequestReturnCacheDataDontLoad = 3,

    NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};
typedef NSUInteger NSURLRequestCachePolicy;
0

, ASICacheDelegate.h , :

// Cache storage policies control whether cached data persists between application launches (ASICachePermanentlyCacheStoragePolicy) or not (ASICacheForSessionDurationCacheStoragePolicy)
// Calling [ASIHTTPRequest clearSession] will remove any data stored using ASICacheForSessionDurationCacheStoragePolicy
typedef enum _ASICacheStoragePolicy {
    ASICacheForSessionDurationCacheStoragePolicy = 0,
    ASICachePermanentlyCacheStoragePolicy = 1
} ASICacheStoragePolicy;

Originate , AFNetworking NSURLCache.

, ,

, AFNetworking

, ASI, , ( ).

NSURLCache ( reset ) , .

, NSURLCache ASICacheForSessionDurationCacheStoragePolicy.

ASICachePermanentlyCacheStoragePolicy:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:2 * 1024 * 1024
                                                        diskCapacity:100 * 1024 * 1024
                                                            diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];

, - [ASIHTTPRequest clearSession] , " NSURLCache Ultimate Control".

AFAIK, , API AFNetworking, :)

0
source

Source: https://habr.com/ru/post/1543713/


All Articles