在网络应用程序中,经常需要多任务连接来提高程序的性能。比如多任务下载,多任务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];
}
- (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的大小,而不是无限制的使用任务。当一个任务完成,就执行待机中的任务。
分享到:
相关推荐
iOS开发 - 第04篇 - 网络 - 01 - NSOperation & 网络基础,对应博客地址:http://blog.csdn.net/nsryan
配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加到NSOperationQueue中 ...
NSOperation 网络演示 关于如何使用 NSOperation 子类进行异步网络调用的小演示。 这只是一个演示,不应被视为完整/生产就绪/测试
iOS-多线程之NSOperation - iOS知识库1
ios 多线程NSoperation
Objective-C的NSOperation多线程类基本使用指南剖析.pdfObjective-C的NSOperation多线程类基本使用指南剖析.pdfObjective-C的NSOperation多线程类基本使用指南剖析.pdfObjective-C的NSOperation多线程类基本使用指南...
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个NSOperation对象中 (2)然后将NSOperation...
NSOperation为tableviewcell加载数据
NSInvocationOperationViewController: NSOperation 的子类 NSInvocationOperation 线程的基本使用 NSBlockOperationViewController: NSOperation 的子类 NSBlockOperation 线程的基本使用 ...
OC-NSOperation异步线程下载图片
Demo讲解了同步和异步的NSOperation的示例,更多的参见我的博客
一个NSOpration的例子,转载自raywenderlich
NSOperation本身是抽象基类,因此必须使用它的子类,使用NSOperation子类的方式有2种: 1> Foundation框架提供了两个具体子类直接供我们使用:NSInvocationOperation和NSBlockOperation 2> 自定义子类继承NSOperation...
多线程加载图片
iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
想在开发过程中,学习使用线程的朋友可以下来看看。里面涵盖了 几乎所有线程使用的类型。而且对这几种 类进行了很细致的解释。包括 定义,功能,和其他的比较。很不错的
Multi-ImagesDownload 本示例代码利用NSOperation实现了tableView中的多图片下载功能. 了解详情请戳:.