Ios-asihttprequest caching mechanism

Source: Internet
Author: User

third-party network request Library

* We can use the nsurlrequest and nsurlconnection provided by the system when we request the network, it basically can realize our basic function.

* But sometimes we use third-party encapsulated libraries, which makes it easy to implement features that are difficult to implement using system methods.

* Common third-party libraries are: ASIHTTPRequest, afnetworking, 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 has a detailed introduction.

* Today, take asihttprequest as an example to achieve a local cache of data.

Why use local cache?

* The most understandable explanation is the user experience.

* Access to previously accessed data can be accessed without the use of a network.

* Access to data, if there is data in the cache, do not send a request to the server, you can save user traffic.

asihttprequest caching mechanism

The information in this picture is obtained through the network request, if the next time you open the app without the network, we also want to show the above data, is to use the caching mechanism, how is the caching mechanism implemented?

Basic Principles of Caching:

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

* Set to download cache when network request. The cached data is stored in the specified location, and we can read the cached data based on the cache policy.

Attention:

    • When using ASIHTTPRequest, we will first add this third-party class library to our project.
    • We're going to add the appropriate framework to the file.
    • This class library, does not support arc, if you are under ARC, you need to set the relevant information. Open: target-"Build phases=>compile Sources. Double-click the corresponding. m file. In the box that pops up, enter-fno-objc-arc.

The following code probably means:

Set up cache classes, cache paths, set cache policies (detailed later), assign cache classes to a property;

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 will be able to initiate the request:

Request path; Set cache for requested data; Storage Policy (described later); initiate a request; obtain the requested 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;
 caching policies and storage policies

The cache policy, the storage policy, is known as the name that the cache is the policy to take and the strategy that the store is taking.

For example, when caching is used, the cache is read first, the network is requested first, or the cache is read after a failed read.

Whether the storage policy is to take temporary storage or permanent storage.

The following diagram records the ASI cache policy, a variety of caching policies can be used, such as the above code of the cache policy is used in conjunction with the use of the server first request data, failed to load the local cache.

About storage policies: There are probably two types of

Ios-asihttprequest caching mechanism

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.