iOS學習系列 - 利用ASIHTTPRequest實現異步隊列


      做過iOS開發應該都有見過ASIHTTPRequest這個強大的HTTP網絡請求類庫,今天主要來介紹利用ASIHTTPRequest實現異步隊列。

官方地址:http://allseeing-i.com/ASIHTTPRequest/

github地址:http://github.com/pokeb/asi-http-request/tree

      里面具體可以進行HTTP常見的使用,Get,Post,同步,異步的方式進行請求,里面內嵌一些請求方式,例如ASIWebPageRequest,即WebPage呈現方式;ASIS3Request,即Amazon Simple Storage Service (Amazon S3,http://aws.amazon.com/s3/);ASICloudFilesRequest,即Cloud Files CDN,(http://www.rackspace.com/),這些Request都是繼承基礎Request,ASIHTTPRequest,包括POST方式,ASPFormDataRequest,里面還內置權限驗證,SSL驗證以及緩存請求的眾多功能,這些功能有興趣可以下載源代碼參考,今天我介紹它的一種Queue的機制,即通過將異步請求放入隊列中,進行異步調用,最終隊列完后產生一個Queue回調。

      目前,我有一個任務協同辦公的web項目,在客戶端方面,通過iOS請求Server上的任務列表,並且將任務列表Tasklist同步到了我的客戶端的本地數據庫中,在每一個任務列表中都有多個任務,相當於任務列表與任務是一對多的關系,由於我在手機端的操作,我將本地更新(修改、添加、刪除)的任務更新到本地數據庫中,至此我的每一條的任務列表可能都產生了一系列的任務的更新數據,這里不妨這邊定義了一個changeLog的變化記錄類:

@interface ChangeLog : NSManagedObject

@property (nonatomic, retain) NSNumber * changeType;
@property (nonatomic, retain) NSString * taskId;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * value;

@end

這里的taskId就是從server同步下來的任務Id或者本地創建的臨時Id,changeType就是更改的類型,例如它可以表示添加、修改、刪除等等,name表示實體的字段名稱,例如”Subject”,”Body”等等,value就是對應”Subject”,”Body”的值了。

      現在,我想把這些任務的ChangeLog全部同步到Server端,必然在Server上需要相關的API作為支持。假如說,我現在的API的方法是

Sync(String tasklistId, String changes),相當說,我需要請求兩個參數,一個是TasklistId,另外一個在該TasklistId對應的Tasklist上將NSArray的ChangeLog集合,序列化一個JSON字符串,這樣Server端獲取到數據就可以對changes進行反序列化了。


編寫一個HttpWebRequest的類:

#import “ASIHTTPRequest.h”
#import “ASIFormDataRequest.h”
#import “ASINetworkQueue.h”
#import “Reachability.h”

@protocol HttpWebRequestDelegate <NSObject>

- ( void)networkNotReachable;
- ( void)requestFinished:(ASIHTTPRequest *)request;
- ( void)requestFailed:(ASIHTTPRequest *)request;
- ( void)addRequstToPool:(ASIHTTPRequest *)request;
- ( void)queueFinished:(ASINetworkQueue *)queue;
- ( void)notProcessReturned:(NSMutableDictionary*)context;

@end

@interface HttpWebRequest : NSObject

@property(nonatomic,assign)  id<HttpWebRequestDelegate>  delegate;

- (ASIFormDataRequest*)createPostRequest:(NSString*)url
params:(NSMutableDictionary*) params
headers:(NSMutableDictionary*)headers
context:(NSMutableDictionary*)context;

@end

createPostRequest的實現:

- (ASIFormDataRequest*)createPostRequest:(NSString*)url
params:(NSMutableDictionary*) params
headers:(NSMutableDictionary*)headers
context:(NSMutableDictionary*)context
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
if (reachability.currentReachabilityStatus == NotReachable) {
return nil;
}
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
if (context)
{
[request setUserInfo:[NSDictionary dictionaryWithDictionary:context]];
}
if (headers)
{
for (NSString *key  in headers.allKeys)
{
[request addRequestHeader:key value:[headers objectForKey:key]];
}
}
if( params)
{
for(NSString *key  in  params.allKeys)
{
[request setPostValue:[ params objectForKey:key] forKey:key];
}
}
  
// cookies設置
NSHTTPCookieStorage *sharedHTTPCookie = [NSHTTPCookieStorage sharedHTTPCookieStorage];
// [request setUseCookiePersistence:YES];
[request setRequestCookies: [NSMutableArray arrayWithArray:sharedHTTPCookie.cookies]];
  
[request setTimeOutSeconds:SYSTEM_REQUEST_TIMEOUT];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy];
  
[request setValidatesSecureCertificate:NO];
return request;
}

這里我用到一個ASIFormDataRequest的POST請求

再定義一個TaskService類:

- ( void)syncTasks:(NSMutableArray*)tasklists
context:(NSMutableDictionary*)context
delegate:( id) delegate
{

}

這里將本地數據庫的ChangeLog變動提交到服務端,假定tasklists已經是從本地數據庫取出來的待提交的數據

在開始實現之前,我們可能會這樣寫:

for (Tasklist *tasklist  in tasklists)
{
// 前面進行一些列的本地數據庫操作
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data setObject:tasklistId forKey:@”tasklistId”];
[data setObject:changeLogsJson forKey:@”changes”];

HttpWebRequest *request = [[HttpWebRequest alloc] init];
ASIFormDataRequest *postRequest = [request createPostRequest:url  params:data headers:nil context:context1];

[request startAsynchronous];
}

startAsynchronous方法就進行一次異步調用,並且通過

- ( void)requestFinished:(ASIHTTPRequest *)request;

方法進行完成回調。

      但是,這樣就會產生一個問題,當你異步請求一個sync_url的時候,這里假如有10條的tasklist,就會產生10條的async,但是你該如何確保它10條都已經執行完畢呢,你也許會想到使用一個原子鎖,通過@property (atomic, retain) counter;來實現,但這里,我使用了ASIHTTPRequest內置一個方式就是ASINetworkQueue隊列的方式來實現一個Queue的Finish的回調。

現在我重寫定義syncTasks的方法:

- ( void)syncTasks:(NSMutableArray*)tasklists
context:(NSMutableDictionary*)context
queue:(ASINetworkQueue*)networkQueue
delegate:( id) delegate
{

}

這里我多了一個ASINetworkQueue為類型的參數,現在我改寫syncTasks的實現:

networkQueue. delegate =  delegate;
networkQueue.queueDidFinishSelector = @selector(queueFinished:);
networkQueue.requestDidFinishSelector = @selector(requestFinished:);
networkQueue.requestDidFailSelector = @selector(requestFailed:);

for (Tasklist *tasklist  in tasklists)
{
// 前面進行一些列的本地數據庫操作
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data setObject:tasklistId forKey:@”tasklistId”];
[data setObject:changeLogsJson forKey:@”changes”];

HttpWebRequest *request = [[HttpWebRequest alloc] init];
ASIFormDataRequest *postRequest = [request createPostRequest:url  params:data headers:nil context:context1];

if(postRequest == nil) {
[ delegate networkNotReachable];
break;
}
[networkQueue addOperation:postRequest];
[request release];
}

[networkQueue go];

      首先,對networkQueue進行一些初始化,例如加入委托,隊列完成回調,請求完成回調,錯誤回調等等,在forin語句中,將每條的postRequest請求加入到該異步隊列中[networkQueue addOperation:postRequest];最后通過[networkQueue go];開始執行隊列中的異步請求

這樣你就可以在所有request完成之后,即requestFinished回調完成之后,繼續執行queueFinished的回調。

這樣就實現了一個異步請求的隊列。

最后調用的代碼入口:

NSMutableDictionary *context = [NSMutableDictionary dictionary];
[context setObject:@”SyncTasks” forKey:REQUEST_TYPE];
if(networkQueue)
{
[networkQueue reset];
}
networkQueue = [ASINetworkQueue queue];
[taskService syncTasks:tempTasklists context:context queue:networkQueue  delegate:self];



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM