IOS-ASIHTTPRequest cache mechanism, ios-asihttprequest

Source: Internet
Author: User

IOS-ASIHTTPRequest cache mechanism, ios-asihttprequest
Third-party network request library

* For network requests, we can use the NSURLRequest and NSURLConnection provided by the system to implement our basic functions.

* However, sometimes we use third-party encapsulated libraries to easily implement functions that are difficult to implement using the system method.

* Common third-party libraries include ASIHTTPRequest, AFNetworking, and MKNetworkKit.

  • ASIHTTPRequest: http://allseeing-i.com/ASIHTTPRequest/
  • AFNETworking: https://github.com/AFNetworking/AFNetworking
  • MKNetworkKit: https://github.com/MugunthKumar/MKNetworkKit

* For the basic use of third-party libraries, the official website will provide a detailed introduction.

* Today, we will use ASIHTTPRequest as an example to implement local data caching.

 

Why use local cache?

* The most easy-to-understand explanation is the user experience.

* If no network is used, you can access previously accessed data.

* Access data. If there is data in the cache, you do not need to send requests to the server, which can save your traffic.

  ASIHTTPRequest Cache Mechanism

The information in this figure is obtained through a network request. If you do not use the network when you open the APP next time, you must use the cache mechanism to display the above data, how is the cache mechanism implemented?

Basic Principles of Caching:

* Create a cache region, set the cache path, and set the cache policy.

* Set as the download cache for network requests. The cached data is stored in the specified location. We can read the cached data according to the cache policy.

Note:

  • When using ASIHTTPRequest, we must first add this third-party class library to our project.
  • We want to add the corresponding framework to the file.
  • This class library does not support ARC. If you are under ARC, you need to set relevant information. Open target-> Build Phases => Compile Sources. Double-click the corresponding. m file. In the pop-up box, enter-fno-objc-arc.

The following code roughly indicates:

Create a cache class; set the cache path; set the Cache Policy (described later); assign the cache class to an attribute;

ASIDownloadCache *cache = [[ASIDownloadCache alloc]init];NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];NSString *filePath = [documentsPath stringByAppendingPathComponent:@"CacheFiled"];[cache setStoragePath:filePath];[cache setDefaultCachePolicy:ASIAskServerIfModifiedCachePolicy |ASIFallbackToCacheIfLoadFailsCachePolicy];[self setMyCache:cache];

 Next, we can initiate a request:

Request Path; request data setting cache; Storage Policy (described later); Initiate request; GET request data;

NSURL *url = [NSURL URLWithString:@"http://apis.juhe.cn/cook/query?key=c84890ed990675f3c454cb2e86fc0a75&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=3"];ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:url];[request setDownloadCache:[self myCache]];[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];[request startSynchronous];NSData *data = request.responseData;
Cache Policy and Storage Policy

The cache policy and storage policy, known as the Cache Policy and the storage policy.

For example, when cache is used, whether to read the cache first, request the network first, or read the cache after Network Reading fails.

The storage policy is temporary storage or permanent storage.

The following figure shows the ASI Cache Policy. Multiple cache policies can be used in combination. For example, the Cache Policy of the above code is used in combination to request data from the server first, load the local cache upon failure.

There are two storage policies:

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.