iOS-NSURLCache記憶體緩衝

來源:互聯網
上載者:User

標籤:

在IOS應用程式開發中,為了減少與服務端的互動次數,加快使用者的響應速度,一般都會在IOS裝置中加一個緩衝的機制。使用緩衝的目的是為了使用的應用程式能更快速的響應使用者輸入,是程式高效的運行。有時候我們需要將遠程web伺服器擷取的資料緩衝起來,減少對同一個url多次請求。下面將介紹如何在IOS裝置中進行緩衝。

 

 記憶體緩衝我們可以使用sdk中的NSURLCache類。NSURLRequest需要一個緩衝參數來說明它請求的url何如快取資料的,我們先看下它的CachePolicy類型。

 

 1、NSURLRequestUseProtocolCachePolicy NSURLRequest預設的cache policy,使用Protocol協議定義。

 

 2、NSURLRequestReloadIgnoringCacheData 忽略緩衝直接從原始地址下載。

 

 3、NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data時才從原始地址下載。

 

 4、NSURLRequestReturnCacheDataDontLoad 只使用cache資料,如果不存在cache,請求失敗;用於沒有建立網路連接離線模式;

 

 5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和遠端快取資料,直接從原始地址下載,與NSURLRequestReloadIgnoringCacheData類似。

 

 6、NSURLRequestReloadRevalidatingCacheData:驗證本機資料與遠端資料是否相同,如果不同則下載遠端資料,否則使用本機資料。

 

 NSURLCache還提供了很多方法,來方便我們實現應用程式的緩衝機制。下面我通過一個例子來說明,這個例子減少我們對同一個url多次請求。看下面代碼:

#import

 

@interface ViewController : UIViewController

 

@property (strong, nonatomic) NSURLConnection *connection;

@property (strong, nonatomic) NSURLCache *urlCache;

@property (strong, nonatomic) NSURL *url;

@property (strong, nonatomic) NSMutableURLRequest *request;

 

- (IBAction)reloadWebView:(UIButton *)sender;

 

@end

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    NSString *paramURLAsString= @"http://blog.sina.com.cn/u/2526279194";

    self.urlCache = [NSURLCache sharedURLCache];

 

    [self.urlCache setMemoryCapacity:1*1024*1024];

    //建立一個nsurl

    self.url = [NSURL URLWithString:paramURLAsString];

    //建立一個請求

    self.request=[NSMutableURLRequest requestWithURL:self.url

                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy

                                   timeoutInterval:30.0f];

    [self.myWebView loadRequest:self.request];

}

 

這個例子中,我們請求url為http://blog.sina.com.cn/u/2526279194的網站。如果這個url被緩衝了,我們直接從緩衝中擷取資料,否則從http://blog.sina.com.cn/u/2526279194網站上重新擷取資料。我們設定了緩衝大小為1M。

 

- (IBAction)reloadWebView:(UIButton *)sender {

 

    //從請求中擷取緩衝輸出

    NSCachedURLResponse *response =[self.urlCache cachedResponseForRequest:self.request];

    //判斷是否有緩衝

    if (response != nil){

        NSLog(@"如果有緩衝輸出,從緩衝中擷取資料");

        [self.request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];

    }

    [self.myWebView loadRequest:self.request];

    

    self.connection = nil;

   

    NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:self.request

                                                                     delegate:self

                                                             startImmediately:YES];

    self.connection = newConnection;

}

 

使用下面代碼,我將請求的過程列印出來

- (void)  connection:(NSURLConnection *)connection

  didReceiveResponse:(NSURLResponse *)response{

    NSLog(@"將接收輸出");

}

- (NSURLRequest *)connection:(NSURLConnection *)connection

             willSendRequest:(NSURLRequest *)request

            redirectResponse:(NSURLResponse *)redirectResponse{

    NSLog(@"即將發送請求");

    return(request);

}

- (void)connection:(NSURLConnection *)connection

    didReceiveData:(NSData *)data{

    NSLog(@"接受資料");

    NSLog(@"資料長度為 = %lu", (unsigned long)[data length]);

}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection

                  willCacheResponse:(NSCachedURLResponse *)cachedResponse{

    NSLog(@"將緩衝輸出");

    return(cachedResponse);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@"請求完成");

}

- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error{

    NSLog(@"請求失敗");

}

 

@end

 

第一次列印結果如下

 

2013-01-31 15:28:29.923 NSURLCacheDemo[27848:907] 即將發送請求

2013-01-31 15:28:30.043 NSURLCacheDemo[27848:907] 將接收輸出

2013-01-31 15:28:30.045 NSURLCacheDemo[27848:907] 接受資料

2013-01-31 15:28:30.047 NSURLCacheDemo[27848:907] 資料長度為 = 30047

2013-01-31 15:28:30.095 NSURLCacheDemo[27848:907] 接受資料

2013-01-31 15:28:30.098 NSURLCacheDemo[27848:907] 資料長度為 = 3575

2013-01-31 15:28:30.102 NSURLCacheDemo[27848:907] 接受資料

2013-01-31 15:28:30.104 NSURLCacheDemo[27848:907] 數據長度為 = 1482

2013-01-31 15:28:30.105 NSURLCacheDemo[27848:907] 將緩衝輸出

2013-01-31 15:28:30.107 NSURLCacheDemo[27848:907] 請求完成

第二次點擊列印結果如下

2013-01-31 15:28:31.599 NSURLCacheDemo[27848:907] 如果有緩衝輸出,從緩衝中擷取資料

2013-01-31 15:28:31.607 NSURLCacheDemo[27848:907] 即將發送請求

2013-01-31 15:28:31.840 NSURLCacheDemo[27848:907] 將接收輸出

2013-01-31 15:28:31.843 NSURLCacheDemo[27848:907] 接受資料

2013-01-31 15:28:31.845 NSURLCacheDemo[27848:907] 資料長度為 = 35104

2013-01-31 15:28:31.846 NSURLCacheDemo[27848:907] 請求完成

我們看到沒有“將緩衝輸出”一項,請求到的資料是第一次請求的累積,也就是第二次是從記憶體中擷取資料的。

 

 

iOS-NSURLCache記憶體緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.