您的位置:首页 > 运维架构

关于NSOperation

2011-08-24 18:29 337 查看
1,operationQueue 里边应该可以同时添加多个operation吧?

是的,本来operationQueue的目的就是多线程管理,那多线程,可只是一个线程。

而且我们可以设置这个队列每次被处理的“操作”数量

NSOperationQueue *aQ = [[NSOperationQueue alloc] init];
[aQ setMaxConcurrentOperationCount:10];


这里的setMaxConcurrentOperationCount就是同时被处理的“操作数”,参数为整数int或NSInteger (两个是一样的,不记得的可以在我的博客里面搜索一下噢~)

2,那main函数应该怎么写?

main函数中其实只需要写你要在另外一个进程里面作的事情。比如对于我来说,我常常只是作一个简单的事情,那我会用NSInvocationOperation,NSOperation的简化版。比如说:

NSInvocationOperation *aOpt = [[NSInvocationOperation alloc]
initWithTarget:self selector:@selector(doSomeThing) object:nil];

- (void)doSomeThing
{
//读取大量大延迟数据等等
//可以使用performSelectorOnMainThread来将得来的数据返回到主线程
}


在doSomeThing函数里面,我可以从网上读取一些东西,但是读取是需要占用时间,而堵塞主线程的。而使用NSOperation这样使用就不会了。

而如果是NSOperation,虽然复杂了一些,又是做一个NSOperation的子类。其实main函数做得事情和doSomeThing是一抹一样的。只不过如果你制作这个子类,你对其操作的内容可以更多,可以制作更复杂的读取,载入操作等等,而且你可以重复使用这个类功能阿。再者,NSOperation也提供了对runtime操作的支持,不过那就太麻烦了,一般不大用的上。

================================================================================================================

多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。

本次讲解NSOperation的使用方法:

1,将想在另外一个线程的工作单独成类,并设置其父类为NSOperation:

@interface ImageLoadingOperation : NSOperation {
NSURL *imageURL; //这个例子里面需要传入一个图片地址,所以定义一个NSURL变量
id target; //由于需要返回一些值,所以需要一个对象参数返回要被返回的对象(运行此线程的类对象)
SEL action; //返回值要激发的方法函数
}


2,借由其初始化方法来传入所需要的参数和对象

- (id)initWithImageURL:(NSURL *)theImageURL target:(id)theTarget action:(SEL)theAction
{
self = [super init]; //在老帖里面解释过为什么需要这么做了
if (self) {
imageURL = [theImageURL retain]; // 拷贝进对象,并retain(为什么?请查老帖)
target = theTarget;
action = theAction;
}
return self;
}


呼叫这个类对象的时候,传入所需要的参数和对象

// 这些是需要对其初始化的类中的代码
ImageLoadingOperation *operation = [[ImageLoadingOperation alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImageWithResult:)];  //初始化
[operationQueue addOperation:operation]; //添加到运行队列
[operation release];  //由于队列对其retain,所以我们需要release它


3,在我们的线程操作类中的main函数执行所需要的工作

- (void)main
{
// 同时载入图片
NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];

// 打包返回给初始类对象,然后执行其指定的操作
NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:image, ImageResultKey, imageURL, URLResultKey, nil];
[target performSelectorOnMainThread:action withObject:result waitUntilDone:NO];

[data release]; //不需要了就清理
[image release];
}


这些就是一个简单的NSOperation的使用过程了。其实看看嘛,非常简单的,正如苹果为我们准备的其他API一样!

==================================================一个例子=================================================

1、将想在另外一个线程的工作单独成类,并设置其父类为NSOperation

Ios代码


@interface ImageLoadingOperation : NSOperation {

//需要传入一个图片地址,所以定义一个NSURL变量

NSURL *imageURL;

//由于需要返回一些值,所以需要一个对象参数返回要被返回的对象(运行此线程的类对象)

id target;

//返回值要激发的方法函数

SEL action;

}

2、借由其初始化方法来传入所需要的参数和对象

Ios代码


- (id)initWithImageURL:(NSURL *)theImageURL target:(id)theTarget action:(SEL)theAction

{

self = [super init];

if (self) {

//拷贝进对象,并retain

imageURL = [theImageURL retain];

target = theTarget;

action = theAction;

}

return self;

}

初始化这个类的时候,传入所需要的参数和对象

Ios代码


//这些是需要对其初始化的类中的代码

//初始化

ImageLoadingOperation *operation = [[ImageLoadingOperation alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImageWithResult:)];

//添加到运行队列

[operationQueue addOperation:operation];

//由于队列对其retain,所以需要release它

[operation release];

3、在线程操作类中的main函数执行所需要的工作

Ios代码


- (void)main

{

//载入图片

NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];

UIImage *image = [[UIImage alloc] initWithData:data];

//打包返回给初始类对象,然后执行其指定的操作

NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:image, ImageResultKey, imageURL, URLResultKey, nil];

[target performSelectorOnMainThread:action withObject:result waitUntilDone:NO];

[data release];

[image release];

}

这些就是一个简单的NSOperation的使用过程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: