Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

使用NSOperation+NSOperationQueue实现多任务网络连接
在网络应用程序中,经常需要多任务连接来提高程序的性能。比如多任务下载,多任务HTTP请求等,即线程控制模型中的工作群模型。使用 NSOperation 可以很容易实现这个功能。下面就以使用NSOperation处理并行的HTTP请求为例子,说明其用法。
首先准备一个 NSOperation 的子类,用于处理 HTTP 请求。

@interface RequestOperation : NSOperation {
NSURLRequest* _request;
NSMutableData* _data;
}

- (id)initWithRequest:(NSURLRequest *)request;

@end

下面是实现:

@implementation RequestOperation

- (id)initWithRequest:(NSURLRequest *)request {
if (self = [self init]) {
_request = [request retain];
_data = [[NSMutableData data] retain];
}
return self;
}

- (void)dealloc {
[_request release];
[_data release];
[super dealloc];
}

// 如果不载下面的函数,会出错
- (BOOL)isConcurrent {
return YES;
}

// 开始处理
- (void)start {
if (![self isCancelled]) {
// 以异步方式处理事件
[NSURLConnection connectionWithRequest:_request delegate:self];
}
}

// 取得数据
- (void)connection:(NSURLConnection*)connection
didReceiveData:(NSData*)data {
// 添加数据
[_data appendData:data];
}

// HTTP请求结束
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
}

@end

如果没有重载 isConcurrent 函数,缺省是返回NO,就是说只能以同步的方式处理。而如果又使用了connectionWithRequest:delegate: 以异步方式处理事件后,会产生下面的错误信息:
1
_NSAutoreleaseNoPool(): Object 0x18a140 of class NSURLConnection autoreleased with no pool in place - just leaking

然后在你的 Controller 类中用 NSOperationQueue 来处理各个任务。

@interface xxViewController : UIViewController {
NSOperationQueue* _queue;
}

@end


@implementation xxViewController

- (IBAction)buttonClicked:(id) sender {
_queue = [[NSOperationQueue alloc] init];
// 第一个请求
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
RequestOperation* operation = [[RequestOperation alloc] initWithRequest:request];
[operation autorelease];
// 第二个请求
NSURLRequest* request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.yahoo.co.jp"]];
RequestOperation* operation2 = [[RequestOperation alloc] initWithRequest:request2];
[operation2 autorelease];
// 开始处理
[_queue addOperation:operation];
}

@end

以上,用 NSOperation 来并行处理不同的任务,使用 NSOperationQueue 来控制复数的 NSOperation,并且可以限制Queue的大小,而不是无限制的使用任务。当一个任务完成,就执行待机中的任务。

经过测试,上面的方法不可行,因为在线程中无法使用异步的方式去请求http
改过之后的例子如下

// RequestOperation.m

#import "RequestOperation.h"


@implementation RequestOperation

- (id)initWithURL:(NSString *)url
{
data = [[NSMutableData data] retain];
self = [super init];
if (self)
{
targetURL = url;
}
return self;
}

- (void)showData:(NSData *)theData encoding:(NSString *)encoding {
if (data != theData) {
[data release];
data = [theData retain];

// NSURLResponse's encoding is an IANA string. Use CF utilities to convert it to a CFStringEncoding then a NSStringEncoding
NSStringEncoding nsEncoding = NSUTF8StringEncoding; // default to UTF-8
if (encoding) {
CFStringEncoding cfEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)encoding);
if (cfEncoding != kCFStringEncodingInvalidId) {
nsEncoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
}
}
NSString *displayString = [[NSString alloc] initWithData:data encoding:nsEncoding];
//NSLog(@"body:%@", displayString);
[displayString release];
}
}

- (void)main {
NSLog(@"url:%@", targetURL);
// Synchronously grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:targetURL]];
NSError *error;
NSURLResponse *response;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[self showData:result encoding:[response textEncodingName]];
if (!result) {
NSLog(@"error:",[error localizedDescription]);
}
NSLog(@"%@ is download.", targetURL);
}

- (void)dealloc {
[data release];
[super dealloc];
}

@end

界面部分代码
NSOperationQueue* _queue;
- (IBAction)buttonClicked:(id) sender {
NSLog(@"start.");
_queue = [[NSOperationQueue alloc] init];
[_queue setMaxConcurrentOperationCount:2];
RequestOperation* operation1 = [[RequestOperation alloc] initWithURL:@"http://www.google.com"];
[operation1 autorelease];
RequestOperation* operation2 = [[RequestOperation alloc] initWithURL:@"http://www.yahoo.co.jp"];
[operation2 autorelease];
RequestOperation* operation3 = [[RequestOperation alloc] initWithURL:@"http://new.sina.com.cn"];
[operation3 autorelease];
RequestOperation* operation4 = [[RequestOperation alloc] initWithURL:@"http://www.163.com"];
[operation4 autorelease];
RequestOperation* operation5 = [[RequestOperation alloc] initWithURL:@"http://www.sohu.com"];
[operation5 autorelease];
RequestOperation* operation6 = [[RequestOperation alloc] initWithURL:@"http://www.csdn.net"];
[operation6 autorelease];
RequestOperation* operation7 = [[RequestOperation alloc] initWithURL:@"http://www.yahoo.com.tw"];
[operation7 autorelease];
// 开始处理
[_queue addOperation:operation1];
[_queue addOperation:operation2];
[_queue addOperation:operation3];
[_queue addOperation:operation4];
[_queue addOperation:operation5];
[_queue addOperation:operation6];
[_queue addOperation:operation7];
NSLog(@"end.");
}
<< NSMutableURLRequest实现向web服务器POST JSON数据objective-c对象retain和copy的区别 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.