Third-party library Three20 Network cache mechanism in IOS development

Source: Internet
Author: User

IOS developmentMedium third-party libraryThree20 Network Cache MechanismIs the content to be introduced in this article, mainly to learnThree20NetworkCache MechanismFor more information, see the detailed description in this article.

Prerequisites:

The http-based Last-Modified and ETag can be searched online in detail. Simply put, the server contains an ID or a token when returning the resource.) The client caches the ID, which is contained in the next request to the same resource, the server determines whether the resource is changed based on this ID and returns different results (200 or 304 ).

Three20The default cache solution is as follows:

 
 
  1. TTURLRequestCachePolicyDefault
  2. = (TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk
  3. | TTURLRequestCachePolicyNetwork ),
  4. TTURLRequestCachePolicyNetwork indicates that the Last-Modified policy is used,
  5. TTURLRequestCachePolicyMemory | TTURLRequestCachePolicyDisk indicates the use of memory and File Cache resources and resource IDs,

Change the cache scheme:

 
 
  1. TTURLRequest request;  
  2. //blah,blah  
  3. request.cachePolicy = cachePolicy | TTURLRequestCachePolicyEtag; 

The Etag function is added here. If the server supports it, there is no doubt that this is the best solution. And so on. For example, cache is not required.

How to Use cache:

Here is a piece of TTImageView code. You can see it at a Glance:

 
 
  1. - (void)reload {  
  2. if (nil == _request && nil != _urlPath) {  
  3. UIImage* image = [[TTURLCache sharedCache] imageForURL:_urlPath];  
  4. if (nil != image) {  
  5. self.image = image;  
  6. } else {  
  7.  
  8. TTURLRequest* request = [TTURLRequest requestWithURL:_urlPath delegate:self];  
  9. request.response = [[[TTURLImageResponse alloc] init] autorelease];  
  10. if (![request send]) {  
  11. // Put the default image in place while waiting for the request to load  
  12. if (_defaultImage && nil == self.image) {  
  13. self.image = _defaultImage;  
  14. }  
  15. }  
  16. }  
  17. }  

You can use the TTURLCache Singleton to obtain the local cache of any URL resource. The logic here is as follows:

First, determine whether the image exists in the memory:

 
 
  1. UIImage* image = [[TTURLCache sharedCache] imageForURL:_urlPath] 

If it does not exist, initiate a request and use the Default policy to obtain the image. Assume that the image has been downloaded when the program was opened last time and has been cached on disk. This is the default value), and the image is not changed on the server, and the server supports if-modified, by default, the request returns the image on disk.

For details, see TTURLCache. If you manually send a request, the Default policy can implement the cache mechanism. Some built-in controls, such as TTTableView, are ideal if they contain images.

Summary:IOS developmentMedium third-party libraryThree20NetworkCache MechanismI hope this article will help you!

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.