线程操作 NSOperation以及他的子类与  NSOperationQueue的搭配使用

采用NSOperation(线程操作,通常使用他的子类)和NSOperationQueue(线程列队)搭配来做多线程开发,采用NDOperation中指定的一个操作,把这个指定操作放到线程队列(线程池)中,让线程队列安排他的生命周期。

NSOperation的子类:NSInvocationOperation和NSBlockOperation
NSOperation与NSOperationQueue的搭配有三种方法
一、NSInvocationOperation与NSOperationQueue搭配使用进行对线程开发
//    1、创建视图
    view = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    [self.view addSubview:view];
//    2
、创建线程
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadResource) object:nil];
//     3
、创建线程队列
    NSOperationQueue *operationQueue = [NSOperationQueue new];
//    4
、将线程放到线程队列
    [operationQueue addOperation:invocationOperation];
}

//    5
、在子线程加载网络资源
- (void)loadResource{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
    UIImage *image = [UIImage imageWithData:data];
    6
、回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
       
//        7
、更新UI
        view.image = image;
    }];
   
}

二、NSBlockOperationNSOperationQueue搭配使用,方式一和方式二没有什么本质区别,主要是后者使用block形式进行代码组织,使用相对方便
//   1创建一个视图
    view = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    [self.view addSubview:view];
//    2
、创建一个线程
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
//        5
、加载网络资源
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
        UIImage *image = [UIImage imageWithData:data];
       
//       6
回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//           7
、更新UI
            view.image = image;
        }];
    }];
//   3
创建线程队列
    NSOperationQueue *operationQueue = [NSOperationQueue new];
//   4
将线程放到线程队列里面
    [operationQueue addOperation:blockOperation];
}
三、用于自定义NSOperationNSOperationQueue搭配使用,更加方便封装某一个线程操作。
1、该子类需要重写Main方法,在Main方法内左线程操作,改线程被执行就会自动调用Main方法。
2、在Main方法中切记要新建一个自动释放池,因为如果是同步操作,该方法能够自动访问到主线程的自动释放池,如果是异步执行操作,那么将无法访问到主线程的自动释放池。(同步、异步下一章会讲到)
创建一个以NSOperation为父类的新类,在.m文件中重写Main方法
//
//  CustomOperation.m
//  NSOperation
//
//  Created by 熊永静 on 16/3/3.
//  Copyright © 2016. All rights reserved.
//

#import "CustomOperation.h"
#import
<UIKit/UIKit.h>
#define kurl @
"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@implementation CustomOperation

- (
instancetype)initWithUIImageView:(UIImageView *)imageView
{
   
self = [superinit];
   
if (self) {
       
self.imageView = imageView;
    }
   
returnself
;
}

//重写Main方法
- (
void)main{
   
@autoreleasepool {
//       5在子线程中添加网络资源
       
UIImage *image = [UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:kurl]]];
//        6
       
//       6回到主线程
        [[
NSOperationQueuemainQueue]addOperationWithBlock:^{
           
//           7、更新UI
           
self.imageView.image = image;
        }];

    }

}
@end

在ViewDidLoad里面
//   1创建一个视图
   
view = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,200,200)];
    [
self.viewaddSubview:view];
   
//2、需要创建一个自动释放池,因为在这里无法访问到主线程的自动释放池
   
CustomOperation *customOperation = [[CustomOperationalloc]initWithUIImageView:view];
   
   
//3、创建一个线程队列
   
NSOperationQueue *operationQueue = [NSOperationQueuenew];
   
//    4.将线程操作放到线程队列中
    [operationQueueaddOperation:customOperation];
与NSThread的区别
1、NSThread需要启动,也就是说,需要费心管理线程的声明周期,而NSOperation方式只需要将线程放到线程队列中即可,线程列队负责管理,执行所有线程操作。
2、NSOperation方式管理线程的最大并发数,也就是同时执行的任务数。
3、控制线程的依赖关系,NSOperation之间可以设置依赖来保证执行顺序,比如一定要让操作1执行完后,才能执行操作2,线程之间不能相互依赖,不能App依赖B,B依赖A。
4、线程队列的取消、暂停、恢复。

采用NSOperation(线程操作,通常使用他的子类)和NSOperationQueue(线程列队)搭配来做多线程开发,采用NDOperation中指定的一个操作,把这个指定操作放到线程队列(线程池)中,让线程队列安排他的生命周期。

NSOperation的子类:NSInvocationOperation和NSBlockOperation
NSOperation与NSOperationQueue的搭配有三种方法
一、NSInvocationOperation与NSOperationQueue搭配使用进行对线程开发
//    1、创建视图
    view = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    [self.view addSubview:view];
//    2
、创建线程
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadResource) object:nil];
//     3
、创建线程队列
    NSOperationQueue *operationQueue = [NSOperationQueue new];
//    4
、将线程放到线程队列
    [operationQueue addOperation:invocationOperation];
}

//    5
、在子线程加载网络资源
- (void)loadResource{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
    UIImage *image = [UIImage imageWithData:data];
    6
、回到主线程
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
       
//        7
、更新UI
        view.image = image;
    }];
   
}

二、NSBlockOperationNSOperationQueue搭配使用,方式一和方式二没有什么本质区别,主要是后者使用block形式进行代码组织,使用相对方便
//   1创建一个视图
    view = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    [self.view addSubview:view];
//    2
、创建一个线程
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
//        5
、加载网络资源
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:kurl]];
        UIImage *image = [UIImage imageWithData:data];
       
//       6
回到主线程
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
//           7
、更新UI
            view.image = image;
        }];
    }];
//   3
创建线程队列
    NSOperationQueue *operationQueue = [NSOperationQueue new];
//   4
将线程放到线程队列里面
    [operationQueue addOperation:blockOperation];
}
三、用于自定义NSOperationNSOperationQueue搭配使用,更加方便封装某一个线程操作。
1、该子类需要重写Main方法,在Main方法内左线程操作,改线程被执行就会自动调用Main方法。
2、在Main方法中切记要新建一个自动释放池,因为如果是同步操作,该方法能够自动访问到主线程的自动释放池,如果是异步执行操作,那么将无法访问到主线程的自动释放池。(同步、异步下一章会讲到)
创建一个以NSOperation为父类的新类,在.m文件中重写Main方法
//
//  CustomOperation.m
//  NSOperation
//
//  Created by 熊永静 on 16/3/3.
//  Copyright © 2016. All rights reserved.
//

#import "CustomOperation.h"
#import
<UIKit/UIKit.h>
#define kurl @
"http://store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/AppleInc/aos/published/images/s/38/s38ga/rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp&.v=1454777389943"
@implementation CustomOperation

- (
instancetype)initWithUIImageView:(UIImageView *)imageView
{
   
self = [superinit];
   
if (self) {
       
self.imageView = imageView;
    }
   
returnself
;
}

//重写Main方法
- (
void)main{
   
@autoreleasepool {
//       5在子线程中添加网络资源
       
UIImage *image = [UIImageimageWithData:[NSDatadataWithContentsOfURL:[NSURLURLWithString:kurl]]];
//        6
       
//       6回到主线程
        [[
NSOperationQueuemainQueue]addOperationWithBlock:^{
           
//           7、更新UI
           
self.imageView.image = image;
        }];

    }

}
@end

在ViewDidLoad里面
//   1创建一个视图
   
view = [[UIImageViewalloc]initWithFrame:CGRectMake(50,50,200,200)];
    [
self.viewaddSubview:view];
   
//2、需要创建一个自动释放池,因为在这里无法访问到主线程的自动释放池
   
CustomOperation *customOperation = [[CustomOperationalloc]initWithUIImageView:view];
   
   
//3、创建一个线程队列
   
NSOperationQueue *operationQueue = [NSOperationQueuenew];
   
//    4.将线程操作放到线程队列中
    [operationQueueaddOperation:customOperation];
与NSThread的区别
1、NSThread需要启动,也就是说,需要费心管理线程的声明周期,而NSOperation方式只需要将线程放到线程队列中即可,线程列队负责管理,执行所有线程操作。
2、NSOperation方式管理线程的最大并发数,也就是同时执行的任务数。
3、控制线程的依赖关系,NSOperation之间可以设置依赖来保证执行顺序,比如一定要让操作1执行完后,才能执行操作2,线程之间不能相互依赖,不能App依赖B,B依赖A。
4、线程队列的取消、暂停、恢复。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值