12五/108
iPhone开发技巧之网络篇(3)— 使用NSOperation建立多任务网络连接
iPhone开发技巧之网络篇(3)--- 使用NSOperation建立多任务网络连接
在网络应用程序中,经常需要多任务连接来提高程序的性能。比如多任务下载,多任务HTTP请求等,即线程控制模型中的工作群模型。使用 NSOperation 可以很容易实现这个功能。下面就以使用NSOperation处理并行的HTTP请求为例子,说明其用法。
首先准备一个 NSOperation 的子类,用于处理 HTTP 请求。
1 2 3 4 5 6 7 8 |
@interface RequestOperation : NSOperation { NSURLRequest* _request; NSMutableData* _data; } - (id)initWithRequest:(NSURLRequest *)request; @end |
下面是实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
@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 来处理各个任务。
1 2 3 4 5 |
@interface xxViewController : UIViewController { NSOperationQueue* _queue; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@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的大小,而不是无限制的使用任务。当一个任务完成,就执行待机中的任务。
相关文章
- iPhone开发技巧之发布篇(7)--- 制作自己的Cydia发布源 - (2012-01-20)
- iPhone开发技巧之发布篇(6)--- 不需Developper认证的真机调试方法 - (2011-12-25)
- iPhone开发技巧之环境篇(11) --- 让Xcode对应多个版本的iOS SDK - (2011-12-03)
- iPhone开发技巧之发布篇(5)--- 在程序中添加广告 - (2011-11-20)
- iPhone开发技巧之环境篇(10)--- 在控制台调试iPhone应用程序 - (2011-11-13)
- iPhone开发技巧之调试篇(3)--- 程序Crash后的调试技巧 - (2011-11-06)
- iPhone开发技巧之环境篇(9)--- Xcode中的注释 - (2011-01-12)
- iPhone开发技巧之数据篇(2)--- iPhone程序中的加密处理 - (2011-01-10)
- iPhone开发技巧之发布篇(4)--- 使用 Ad Hoc 发布自己的应用程序 - (2010-07-22)
- iPhone开发技巧之发布篇(3)--- 你的程序被拒了吗? - (2010-07-19)
- iPhone开发技巧之发布篇(2)--- 税务相关手续 - (2010-07-16)
- iPhone开发技巧之发布篇(1)--- 登录银行信息 - (2010-07-12)
- iPhone开发技巧之工具篇(4)--- 使用afconvert转换WAV文件 - (2010-06-24)
- iPhone开发技巧之私有API(8)--- UIApplication - (2010-06-18)
- iPhone开发技巧之私有API(7)--- 用UIWebView访问BASIC认证的页面 - (2010-06-16)
- iPhone开发技巧之私有API(6)--- 设置UIWebView中的User-Agent - (2010-06-14)
- iPhone开发技巧之私有API(5)--- UISegmentedControl - (2010-06-11)
- iPhone开发技巧之私有API(4)--- UIBarButtonItem - (2010-06-09)
- iPhone开发技巧之私有API(3)--- UIButton - (2010-06-07)
- iPhone开发技巧之数据篇(1)--- 使用正则表达式 - (2010-06-04)
- iPhone开发技巧之私有API(2)--- UITableView - (2010-06-01)
- iPhone开发技巧之私有API(1) --- 设备相关信息 - (2010-05-28)
- iPhone开发技巧之网络篇(5)--- 使用libcurl连接https服务器 - (2010-05-17)
- iPhone开发技巧之网络篇(4)--- 确认网络环境 3G/WIFI - (2010-05-14)
- iPhone开发技巧之网络篇(2)--- Web服务 - (2010-04-20)
2010年05月12日 18:27
有没有比较完整的示例代码阿,希望可以分享一下
winwys@gmail.com
2010年05月12日 18:38
给你一个链接吧,你下了看看就知道了。
http://journal.mycom.co.jp/column/iphone/006/resources/RSS-1.zip
2010年05月17日 14:34
operation2 没有往_queue 里加啊?
2010年05月17日 17:24
是啊,这个需要加!
2010年07月13日 22:45
应当是-main 函数
2010年07月15日 16:23
非常感谢,一直在为收不到消息烦恼,看了你的文章豁然开朗,果然是
- (BOOL)isConcurrent {
return YES;
}
的问题。原来线程默认是同步方式的啊。虽然还不是很理解,不过问题是解决了。
再次感谢!
2010年08月16日 17:02
start 是谁来执行的,还有就是初始化里是不是应该是 self =[super init]?
2010年08月16日 17:05
还有就是 isCancelled 那里来的?