Some knowledge points in AFNetworking 3.1.0, afnetworking3.1.0
# POST/GET requests
/*!
First, you must know that POST requests cannot be cached, and only GET requests can be cached. From a mathematical point of view, the GET result is idempotent, as if the keys and values in the dictionary are idempotent, and the POST is idempotent. The Caching logic is to take the value composed of the query parameters as the key and the corresponding result as the value. In this sense, the resource link of a file is also called a GET request.
80% of cache requirements: two lines of code can meet
To set the cache, you only need to perform the following three steps:
Step 1: use GET requests.
Step 2:
If you have used the GET request, the iOS SDK has completed the cache for you. You only need to set the memory cache size, disk cache size, and cache path. Even the two lines of code are not set, there is a default value. The Code is as follows:
Note:
Starting from iOS 5.0, it supports disk caching, but only supports HTTP
Starting from iOS 6.0 and supporting HTTPS caching
*/
NSURLCache * urlCache = [[NSURLCache alloc] initWithMemoryCapacity: 4*1024*1024 diskCapacity: 20*1024*1024 diskPath: nil];
[NSURLCache setSharedURLCache: urlCache];
#########
- /**
- * Cancel all network requests
- * A finished (or canceled) operation is still given a chance to execute its completion block before it iremoved from the queue.
- */
- + (Void) cancelAllRequest
- {
- [[BJAppClient sharedClient]. operationQueue cancelAllOperations];
- }
- # Pragma mark-cancel the specified url request/
- /**
- * Cancel the specified url request.
- *
- * @ Param requestType the request type of the request
- * @ Param string the complete url of the request
- */
- + (Void) cancelHttpRequestWithRequestType :( NSString *) requestType
- RequestUrlString :( NSString *) string
- {
- NSError * error;
- /** Create an NSMutableURLRequest based on the request type and the request url --- use this url to match whether the url exists in the Request queue. If yes, cancel the request */
- NSString * urlToPeCanced = [[[BJAppClient sharedClient]. requestSerializer
- RequestWithMethod: requestType URLString: string parameters: nil error: & error] URL] path];
- For (NSOperation * operation in [BJAppClient sharedClient]. operationQueue. operations ){
- // Request queue
- If ([operation isKindOfClass: [NSURLSessionTask class]) {
- // Request type match
- BOOL hasMatchRequestType = [requestType is1_tostring: [[(NSURLSessionTask *) operation currentRequest] HTTPMethod];
- // Request url match
- BOOL hasMatchRequestUrlString = [urlToPeCanced isEqualToString: [[(NSURLSessionTask *) operation currentRequest] URL] path];
- // Cancel the request if both match
- If (hasMatchRequestType & hasMatchRequestUrlString ){
- [Operation cancel];
- }
- }
- }
- }
Related links:
Https://github.com/boai/BANetManagerhttp://www.jianshu.com/p/6856bd9050fchttp://blog.csdn.net/heberan/article/details/51567165NSURLCachehttp://www.cnblogs.com/cbw1987/p/5910624.html