Some knowledge points in AFNetworking 3.1.0, afnetworking3.1.0

Source: Internet
Author: User

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];

#########

  1. /**
  2. * Cancel all network requests
  3. * A finished (or canceled) operation is still given a chance to execute its completion block before it iremoved from the queue.
  4. */
  5. + (Void) cancelAllRequest
  6. {
  7. [[BJAppClient sharedClient]. operationQueue cancelAllOperations];
  8. }
  9. # Pragma mark-cancel the specified url request/
  10. /**
  11. * Cancel the specified url request.
  12. *
  13. * @ Param requestType the request type of the request
  14. * @ Param string the complete url of the request
  15. */
  16. + (Void) cancelHttpRequestWithRequestType :( NSString *) requestType
  17. RequestUrlString :( NSString *) string
  18. {
  19. NSError * error;
  20. /** 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 */
  21. NSString * urlToPeCanced = [[[BJAppClient sharedClient]. requestSerializer
  22. RequestWithMethod: requestType URLString: string parameters: nil error: & error] URL] path];
  23. For (NSOperation * operation in [BJAppClient sharedClient]. operationQueue. operations ){
  24. // Request queue
  25. If ([operation isKindOfClass: [NSURLSessionTask class]) {
  26. // Request type match
  27. BOOL hasMatchRequestType = [requestType is1_tostring: [[(NSURLSessionTask *) operation currentRequest] HTTPMethod];
  28. // Request url match
  29. BOOL hasMatchRequestUrlString = [urlToPeCanced isEqualToString: [[(NSURLSessionTask *) operation currentRequest] URL] path];
  30. // Cancel the request if both match
  31. If (hasMatchRequestType & hasMatchRequestUrlString ){
  32. [Operation cancel];
  33. }
  34. }
  35. }
  36. }

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.