我们通过一个例子介绍一下请求队列使用,我们设计了一个应用,用户点击GO按钮从服务器同时下载两张图片显示在画面中。
我们直接看看主视图控制器ViewController.h代码如下:
#import “ASIHTTPRequest.h”
#import “ASINetworkQueue.h”
#import “NSNumber+Message.h”
#import “NSString+URLEncoding.h”
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (strong) ASINetworkQueue *networkQueue;
- (IBAction)onClick:(id)sender;
@end
我们需要引入ASI框架的两个头文件ASIHTTPRequest.h和ASINetworkQueue.h。其中imageView1和 imageView2是与画面对应的两个图片视图控件。还定义了ASINetworkQueue 类型的networkQueue属性。我们直接看看主视图控制器ViewController.m中点击GO按钮调用方法,代码如下:
- (IBAction)onClick:(id)sender {
if (!_networkQueue) {
_networkQueue = [[ASINetworkQueue alloc] init]; ①
}
// 停止以前的队列
[_networkQueue cancelAllOperations]; ②
// 创建ASI队列
[_networkQueue setDelegate:self];
[_networkQueue setRequestDidFinishSelector:@selector(requestFinished:)]; ③
[_networkQueue setRequestDidFailSelector:@selector(requestFailed:)]; ④
[_networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; ⑤
for (int i=1; i<3; i++) {
NSString *strURL = [[NSString alloc] initWithFormat:
@”http://iosbook3/download.php?email=%@&FileName=test%i.jpg”,
@”<你的iosbook1.com用户邮箱>”,i];
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.tag = i; ⑥
[_networkQueue addOperation:request]; ⑦
}
[_networkQueue go]; ⑧
}
我们再看看它们的回调方法,代码:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
NSError *eror;
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments error:&eror];
if (!resDict) {
UIImage *img = [UIImage imageWithData:data];
if (request.tag ==1) { ①
_imageView1.image = img;
} else {
_imageView2.image = img;
}
} else {
NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];
NSString *errorStr = [resultCodeObj errorMessage];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”错误信息”
message:errorStr
delegate:nil
cancelButtonTitle:@”OK”
otherButtonTitles: nil];
[alertView show];
}
if ([_networkQueue requestsCount] == 0) { ②
[self setNetworkQueue:nil];
}
NSLog(@”请求成功”);
}
- (void)requestFailed:(ASIHTTPRequest *)request ③
{
NSError *error = [request error];
NSLog(@”%@”,[error localizedDescription]);
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@”请求失败”);
}
- (void)queueFinished:(ASIHTTPRequest *)request ④
{
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@”队列完成”);
}
requestFinished:方法是请求对象成功回调方法,因此有两个请求对象它会被调用两次,在第①行代码中我们根据GO按钮点击事件设定的 请求对象的tag属性,来判断是哪个请求对象的回调。进而加载到显示不同的图片视图。第②代码[_networkQueue requestsCount]可以判断队列中请求对象的个数。
//其他。。。
[networkQueueForSong reset];
//下载队列代理方法
[networkQueueForSong setRequestDidFailSelector:@selector(singleDownLoadFail:)];
[networkQueueForSong setRequestDidFinishSelector:@selector(singleDownloadFinished:)];
[networkQueueForSong setRequestDidReceiveResponseHeadersSelector:@selector(downLoadReceiveResponseHeader:)];
[networkQueueForSong setRequestDidStartSelector:@selector(singleDownLoadStart:)];
[networkQueueForSong setQueueDidFinishSelector:@selector(downLoadFinish)];
[networkQueueForSong setDelegate:self];
//设置下载队列属性,设置为1只允许下完一首再下另一首,默认是并行下载不分前后
[networkQueueForSong setMaxConcurrentOperationCount:1];