• 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];


  • 相关阅读:
    CSUST 8.4 早训
    CSUST 8.5 早训
    hdu1542 Atlantis 线段树--扫描线求面积并
    hdu1540 Tunnel Warfare 线段树/树状数组
    hdu1535 Invitation Cards 最短路
    hdu1358 Period KMP
    SQL Server 向数据库中创建表并添加数据
    初次实践数据库--SQL Server2016
    hdu1301 Jungle Roads 最小生成树
    hdu1281 棋盘游戏 二分图最大匹配
  • 原文地址:https://www.cnblogs.com/liping13599168/p/2685890.html
Copyright © 2020-2023  润新知