自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)ASIHttpRequest創(chuàng)建和執(zhí)行request

移動開發(fā) iOS
本文為大家介紹了iOS開發(fā)中ASIHttpRequest如何創(chuàng)建和執(zhí)行request,其中包括同步請求,異步請求,使用Block,使用隊列,取消異步請求等等內(nèi)容。

本文為大家介紹了iOS開發(fā)中ASIHttpRequest如何創(chuàng)建和執(zhí)行request,其中包括同步請求,異步請求,使用Block,使用隊列,取消異步請求等等內(nèi)容。

創(chuàng)建NSOperationQueue,這個Cocoa架構(gòu)的執(zhí)行任務(wù)(NSOperation)的任務(wù)隊列。我們通過ASIHTTPRequest.h的源碼可以看到,此類本身就是一個NSOperation的子類。也就是說它可以直接被放到任務(wù)隊列中并被執(zhí)行。

同步請求

同步請求會在當(dāng)前線程中執(zhí)行,使用error屬性來檢查結(jié)束狀態(tài)(要下載大文件,則需要設(shè)定downloadDestinationPath來保存文件到本地):

  1. - (IBAction)grabURL:(id)sender 
  2.   NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"]; 
  3.   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  4.   [request startSynchronous]; 
  5.   NSError *error = [request error]; 
  6.   if (!error) { 
  7.     NSString *response = [request responseString]; 
  8.   } 

同步請求會阻塞主線程的執(zhí)行,這導(dǎo)致用戶界面不響應(yīng)用戶操作,任何動畫都會停止渲染。

異步請求

下面是最簡單的異步請求方法,這個request會在全局的NSOperationQueue中執(zhí)行,若要進(jìn)行更復(fù)雜的操作,我們需要自己創(chuàng)建NSOperationQueue或者ASINetworkQueue,后面會講到。

  1. - (IBAction)grabURLInBackground:(id)sender 
  2.    NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"]; 
  3.    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  4.    [request setDelegate:self]; 
  5.    [request startAsynchronous]; 
  6.  
  7. - (void)requestFinished:(ASIHTTPRequest *)request 
  8.    // Use when fetching text data 
  9.    NSString *responseString = [request responseString]; 
  10.  
  11.    // Use when fetching binary data 
  12.    NSData *responseData = [request responseData]; 
  13.  
  14. - (void)requestFailed:(ASIHTTPRequest *)request 
  15.    NSError *error = [request error]; 

使用block

在平臺支持情況下,ASIHTTPRequest1.8以上支持block。

  1. - (IBAction)grabURLInBackground:(id)sender 
  2.    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
  3.    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  4.    [request setCompletionBlock:^{ 
  5.       // Use when fetching text data 
  6.       NSString *responseString = [request responseString]; 
  7.  
  8.       // Use when fetching binary data 
  9.       NSData *responseData = [request responseData]; 
  10.    }]; 
  11.    [request setFailedBlock:^{ 
  12.       NSError *error = [request error]; 
  13.    }]; 
  14.    [request startAsynchronous]; 

注意,聲明request時要使用__block修飾符,這是為了告訴block不要retain request,以免出現(xiàn)retain循環(huán),因為request是會retain block的。

使用隊列

創(chuàng)建NSOperationQueue或者ASINetworkQueue隊列,我們還可以設(shè)定最大并發(fā)連接數(shù):maxConcurrentOperationCount

  1. - (IBAction)grabURLInTheBackground:(id)sender 
  2.    if (![self queue]) { 
  3.       [self setQueue:[[[NSOperationQueue alloc] init] autorelease]]; 
  4.       [self queue].maxConcurrentOperationCount = 4; 
  5.    } 
  6.  
  7.    NSURL *url = [NSURL URLWithString:@"http://www.dreamingwish.com"]; 
  8.    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  9.    [request setDelegate:self]; 
  10.    [request setDidFinishSelector:@selector(requestDone:)]; 
  11.    [request setDidFailSelector:@selector(requestWentWrong:)]; 
  12.    [[self queue] addOperation:request]; //queue is an NSOperationQueue 
  13.  
  14. - (void)requestDone:(ASIHTTPRequest *)request 
  15.    NSString *response = [request responseString]; 
  16.  
  17. - (void)requestWentWrong:(ASIHTTPRequest *)request 
  18.    NSError *error = [request error]; 

如果不設(shè)定selector,那么系統(tǒng)會使用默認(rèn)的requestFinished: 和 requestFailed:方法

如果需要對隊列里面的每個request進(jìn)行區(qū)分,那么可以設(shè)定request的userInfo屬性,它是個NSDictionary,或者更簡單的方法是設(shè)定每個request的tag屬性,這兩個屬性都不會被發(fā)送到服務(wù)器。

不要使用request的URL來區(qū)分每個request,因為URL可能會改變(例如重定向),如果需要使用request的URL,使用[request originalURL],這個將永遠(yuǎn)返回第一個url。

對于ASINetworkQueue

ASINetworkQueue是NSOperationQueue的子類,提供更高級的特性(ASINetworkQueue的代理函數(shù)):

  • requestDidStartSelector
    當(dāng)一個request開始執(zhí)行時,這個代理函數(shù)會被調(diào)用。
  • requestDidReceiveResponseHeadersSelector
    當(dāng)隊列中的request收到服務(wù)器返回的頭信息時,這個代理函數(shù)會被調(diào)用。對于下載很大的文件,這個通常比整個request的完成要早。
  • requestDidFinishSelector
    當(dāng)每個request完成時,這個代理函數(shù)會被調(diào)用。
  • requestDidFailSelector
    當(dāng)每個request失敗時,這個代理函數(shù)會被調(diào)用。
  • queueDidFinishSelector
    當(dāng)隊列完成(無論request失敗還是成功)時,這個代理函數(shù)會被調(diào)用。

ASINetworkQueues與NSOperationQueues稍有不同,加入隊列的request不會立即開始執(zhí)行。如果隊列打開了進(jìn)度開關(guān),那么隊列開始時,會先對所有GET型request進(jìn)行一次HEAD請求,獲得總下載大小,然后真正的request才被執(zhí)行。

向一個已經(jīng)開始進(jìn)行的ASINetworkQueue 加入request會怎樣?

如果你使用ASINetworkQueue來跟蹤若干request的進(jìn)度,只有當(dāng)新的request開始執(zhí)行時,總進(jìn)度才會進(jìn)行自適應(yīng)調(diào)整(向后移動)。ASINetworkQueue不會為隊列開始后才加入的request進(jìn)行HEAD請求,所以如果你一次向一個正在執(zhí)行的隊列加入很多request,那么總進(jìn)度不會立即被更新。

如果隊列已經(jīng)開始了,不需要再次調(diào)用[queue go]。

當(dāng)ASINetworkQueue中的一個request失敗時,默認(rèn)情況下,ASINetworkQueue會取消所有其他的request。要禁用這個特性,設(shè)置 [queue setShouldCancelAllRequestsOnFailure:NO]。

ASINetworkQueues只可以執(zhí)行ASIHTTPRequest操作,二不可以用于通用操作。試圖加入一個不是ASIHTTPRequest的NSOperation將會導(dǎo)致拋出錯誤。

取消異步請求

取消一個異步請求(無論request是由[request startAsynchronous]開始的還是從你創(chuàng)建的隊列中開始的),使用[request cancel]即可。注意同步請求不可以被取消。

注意,如果你取消了一個request,那么這個request將會被視為請求失敗,并且request的代理或者隊列的代理的失敗代理函數(shù)將被調(diào)用。如果你不想讓代理函數(shù)被調(diào)用,那么將delegate設(shè)置為nil,或者使用clearDelegatesAndCancel方法來取消request。

clearDelegatesAndCancel 將會首先清除所有的代理和block。

當(dāng)使用ASINetworkQueue時,如果取消了隊列中的一個request,那么隊列中其他所有request都會被取消,可以設(shè)置shouldCancelAllRequestsOnFailure的值為NO來避免這個現(xiàn)象。

安全地控制delegate防止request完成之前代理被釋放

request并不retain它們的代理,所以有可能你已經(jīng)釋放了代理,而之后request完成了,這將會引起崩潰。大多數(shù)情況下,如果你的代理即將被釋放,你一定也希望取消所有request,因為你已經(jīng)不再關(guān)心它們的返回情況了。如此做:

  1. // 代理類的dealloc函數(shù) 
  2. - (void)dealloc 
  3.    [request clearDelegatesAndCancel]; 
  4.    [request release]; 
  5.    ... 
  6.    [super dealloc]; 
  7. }
責(zé)任編輯:閆佳明 來源: dreamingwish
相關(guān)推薦

2013-07-22 14:38:00

iOS開發(fā)ASIHTTPRequ

2013-07-21 18:22:59

iOS開發(fā)ASIHTTPRequ

2013-07-21 18:18:00

iOS開發(fā)ASIHttpRequ

2013-07-21 18:27:15

iOS開發(fā)ASIHTTPRequ

2013-07-22 14:15:17

iOS開發(fā)ASIHTTPRequ

2013-07-22 14:33:15

iOS開發(fā)ASIHTTPRequ

2013-07-22 13:54:32

iOS開發(fā)ASIHTTPRequ

2013-07-21 18:32:13

iOS開發(fā)ASIHTTPRequ

2013-07-22 14:25:29

iOS開發(fā)ASIHTTPRequ

2013-07-22 14:43:57

iOS開發(fā)ASIHTTPRequ

2013-07-22 13:48:55

iOS開發(fā)ASIHTTPRequ使用Cookie

2013-07-22 14:02:17

iOS開發(fā)ASIHTTPRequ

2013-07-22 14:29:35

iOS開發(fā)ASIHTTPRequ

2013-07-21 18:04:22

ASIHttpRequiOS開發(fā)

2013-03-25 14:13:23

iOSASIHTTPRequ

2011-08-22 10:06:38

IOS開發(fā)ASIHTTPRequHTTP 請求

2013-07-22 14:10:26

iOS開發(fā)ASIHTTPRequ

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2017-03-13 14:30:38

Android開發(fā)庫指南

2011-08-12 13:35:23

iPhone文件流ASIHTTPRequ
點贊
收藏

51CTO技術(shù)棧公眾號