当前位置: 首页 > 工具软件 > RequestQueue > 使用案例 >

RequestQueue 请求队列

司寇善
2023-12-01

操作队列

   如果有多个请求的话,可以把请求放到一个队列里,如果有请求顺序要求的话,可以设置每次请求的最大并行数量

一、系统的操作队列

/*

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/book/search?q=harry&apikey=00862fc9947075ac01928f5cbd516104"]];

    //操作队列

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    //最大并行数量  两个两个发起请求

    queue.maxConcurrentOperationCount = 2;

    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

       //data

    }];

 */


二、ASI的操作队列

    ASINetworkQueue *asiQueue = [[ASINetworkQueue alloc] init];

    asiQueue.maxConcurrentOperationCount = 2;

    for (int i = 0; i < 10; i++) {

      ASIHTTPRequest *asiRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/book/search?q=harry&apikey=00862fc9947075ac01928f5cbd516104"]];

        asiRequest.tag = i;

        asiRequest.delegate = self;

        //把请求添加到队列里

        [asiQueue addOperation:asiRequest];

    }

    [asiQueue go];

    

    ASIFormDataRequest *postRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/book/search"]];

    /*

     要传的参数

    //标准参数

    //q=harry

    //apikey=00862fc9947075ac01928f5cbd516104

     image=图片

     */

    [postRequest setPostValue:@"harry" forKey:@"q"];

    [postRequest setPostValue:@"00862fc9947075ac01928f5cbd516104" forKey:@"apikey”];


    //不是标准的参数  文件/数据  参数

    [postRequest setFile:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"] withFileName:@"1.png" andContentType:@"image/png" forKey:@"image];


    //data

    //[postRequest setData:<#(id)#> withFileName:<#(NSString *)#> andContentType:<#(NSString *)#> forKey:<#(NSString *)#>];

    

    //ASIFormDataRequest *asiRequest2;

 

}


-(void)requestFailed:(ASIHTTPRequest *)request

{

    

}


-(void)requestFinished:(ASIHTTPRequest *)request

{

    NSLog(@"%d",request.tag);

}


 类似资料: