关于NSOperation

来源:互联网 发布:卡卡西实力如何知乎 编辑:程序博客网 时间:2024/05/29 06:56

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代码  收藏代码
  1. @interface ImageLoadingOperation : NSOperation {  
  2.     //需要传入一个图片地址,所以定义一个NSURL变量  
  3.     NSURL *imageURL;  
  4.     //由于需要返回一些值,所以需要一个对象参数返回要被返回的对象(运行此线程的类对象)  
  5.     id target;  
  6.     //返回值要激发的方法函数  
  7.     SEL action;   
  8. }  

 

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

 

Ios代码  收藏代码
  1. - (id)initWithImageURL:(NSURL *)theImageURL target:(id)theTarget action:(SEL)theAction  
  2. {  
  3.     self = [super init];  
  4.     if (self) {  
  5.         //拷贝进对象,并retain  
  6.         imageURL = [theImageURL retain];  
  7.         target = theTarget;  
  8.         action = theAction;  
  9.     }  
  10.     return self;  
  11. }  

 

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

 

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

 

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

 

Ios代码  收藏代码
  1. - (void)main  
  2. {  
  3.     //载入图片  
  4.     NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];  
  5.     UIImage *image = [[UIImage alloc] initWithData:data];  
  6.     //打包返回给初始类对象,然后执行其指定的操作  
  7.     NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:image, ImageResultKey, imageURL, URLResultKey, nil];  
  8.     [target performSelectorOnMainThread:action withObject:result waitUntilDone:NO];  
  9.     [data release];   
  10.     [image release];  
  11. }  

 

这些就是一个简单的NSOperation的使用过程。




原创粉丝点击