There are two possible issues with web-requested apps:
1) If the network is not good or the amount of data is large, there will be a problem due to the data arrival is not timely use of the fluid
2) Frequent sending of network requests, may cause unnecessary traffic generation and slow down the problem of mobile phone speed
For problem one, the usual strategy is to defer loading big data requests
For issue two, the appropriate caching strategy is often used to save network overhead
In the network request, the bigger resource, the slice, the video and so on, needs to do some special processing to enhance the app performance
As in the following scenario:
Request a list of data, each containing: string, picture Url,tableview need to display this data
This is designed to multiple requests, the first request gets the list data, and then multiple requests to download all the pictures.
If all the data is loaded and then the UI is displayed, the download of the image is time-consuming and affects the experience of the app.
Solution:
When the first request to get the list data, all the data is displayed, the location of the picture display a placeholder picture
Download the picture resource using an asynchronous network request and update the UI for processing as soon as the picture is downloaded
Example Description:
A. Sending a GET request data to receive a certain number of game character information
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7C/B4/wKiom1bWhw7xRtELAAKVc8ezzQc209.png "title=" screen shot 2016-03-02 pm 2.23.59.png "width=" 601 "height=" 420 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:601px;height:420px; " alt= "Wkiom1bwhw7xrtelaakvc8ezzqc209.png"/>
B. Data source methods for UITableView
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/B4/wKiom1bWh2LBfMZYAADU5Lfnr88227.png "title=" screen shot 2016-03-02 pm 2.25.48.png "width=" 598 "height=" 124 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:598PX;HEIGHT:124PX; " alt= "Wkiom1bwh2lbfmzyaadu5lfnr88227.png"/>
You can see that the content assigned to cell cells for its Model property Heromodel is the model data after the network request
C. Setter method for cell model properties
First look at the code before the lazy load is used:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/B2/wKioL1bWiLjCd_CkAAE8uCPdD48229.png "title=" screen shot 2016-03-02 pm 2.29.28.png "width=" height= "218" border= "0" hspace= "0" vspace= "0" style= "width:600px;height:218px;" alt= "Wkiol1bwiljcd_ckaae8ucpdd48229.png"/>
The last picture display, the direct use of the synchronous request, due to the large picture resources, the UI interface will be stuck
Use deferred-loaded code instead:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7C/B2/wKioL1bWinyxGgttAAKG28KAMVA448.png "title=" screen shot 2016-03-02 pm 2.36.58.png "width=" 701 "height=" 461 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:701px;height:461px; " alt= "Wkiol1bwinyxggttaakg28kamva448.png"/>
Display a placeholder picture first, then request the picture resource asynchronously, and then display the picture after the request is completed
This way, the UI will not stutter even if the network is not smooth or the picture is large
The above solution, in some cases, improves the user experience, but for TableView, there may be some performance problems: when TableView up and down the process, will produce tableview reuse mechanism, meaning that the above code will be repeated execution, It means that the Web request to download the picture will be sent repeatedly
Disk cache:
On the first request, the result of the request is stored locally, and the next time the same request is sent, the cache is fetched directly from the local
Use of the cache:
For data that needs to be loaded frequently, you should use a local cache
The local cache should not be used for data that is frequently updated by the server
For data that is occasionally updated by the server, you should modify the cache policy
You can specify a cache policy to use when you create a Request object
+ (Instancetype) Requestwithurl: (Nsurl *) theURL CachePolicy: (nsurlrequestcachepolicy) CachePolicy TimeoutInterval: ( Nstimeinterval) Timeoutinterval@property (readonly) Nsurlrequestcachepolicy cachepolicyenum{ Nsurlrequestuseprotocolcachepolicy = 0,//cache policy Using Protocol Nsurlrequestreloadignoringlocalcachedata = 1,//Do not use local cache NS Urlrequestreturncachedataelseload = 2,//use disk cache Nsurlrequestreturncachedatadontload = 3,//Use only disk cache, no Network load};typede f Nsuinteger nsurlrequestcachepolicy;
In the deferred loading instance above:
Request task data should use the Nsurlrequestreloadignoringlocalcachedata method
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7C/B4/wKiom1bWip7ARLvrAADrOI1ROLg787.png "title=" screen shot 2016-03-02 pm 2.39.38.png "width=" 651 "height=" 98 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:651px;height:98px; " alt= "Wkiom1bwip7arlvraadroi1rolg787.png"/>
Lazy loading pictures should use the Nsurlrequestreturncacheddataelseload method:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7C/B4/wKiom1bWijTBhbHJAAD2BCAbAu4718.png "title=" screen shot 2016-03-02 pm 2.37.53.png "width=" 647 "height=" border= "0" hspace= "0" vspace= "0" style= "width:647px;height:87px" alt= "Wkiom1bwijtbhbhjaad2bcabau4718.png"/>
Requested cache data, managed through Nsurlcache
Global objects:
+ (Nsurlcache *) Sharedurlcache
Get Cached objects
-(Nscachedurlresponse *) Cachedresponseforrequest: (Nsurlrequest *) request
Cached Object Nscachedurlresponse Properties
@property (readonly, copy) NSData *data@property (readonly, copy) Nsurlresponse *response
Clean up cached data
-(void) Removecachedresponseforrequest: (Nsurlrequest *) request-(void) removeallcachedresponses
Regular cleanup of caches is often required in the app
The Nsurlcache object can manage the cached space, the cached control can be memory, or it can be a disk
@property (readonly) Nsuinteger currentdiskusage@property Nsuinteger diskcapacity@property (readonly) Nsuinteger Currentmemoryusage@property Nsuinteger memorycapacity
Example Description:
Add code in Appdelegate to detect if the disk cache occupies more than half every 5 minutes, and if so, notify the cache of the need to clean up (or, of course, clean it up directly)
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7C/B4/wKiom1bWjb3w9tN3AAGnqtQMmgQ153.png "title=" screen shot 2016-03-02 pm 2.52.57.png "width=" 650 "height=" 308 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:650px;height:308px; " alt= "Wkiom1bwjb3w9tn3aagnqtqmmgq153.png"/>
This article is from the "Ammon" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746745
iOS Network Development (5) Requested cache