• iOS之NSOperation的简单介绍


    NSOperation

    2.1 NSOperation基本使用

    (1)相关概念

    01 NSOperation是对GCD的包装

    02 两个核心概念【队列+操作】

    (2)基本使用

    01 NSOperation本身是抽象类,只能只有它的子类

    02 三个子类分别是:NSBlockOperation、NSInvocationOperation以及自定义继承自NSOperation的类

    03 NSOperation和NSOperationQueue结合使用实现多线程并发

    (3)相关代码

    //  01 NSInvocationOperation

        //1.封装操作

        /*

         第一个参数:目标对象

         第二个参数:该操作要调用的方法,最多接受一个参数

         第三个参数:调用方法传递的参数,如果方法不接受参数,那么该值传nil

         */

        NSInvocationOperation *operation = [[NSInvocationOperation alloc]

                                            initWithTarget:self selector:@selector(run) object:nil];

        //2.启动操作

        [operation start];

    -------------------------------------------------

        //  02 NSBlockOperation

        //1.封装操作

        /*

         NSBlockOperation提供了一个类方法,在该类方法中封装操作

         */

        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{

            //在主线程中执行

            NSLog(@"---download1--%@",[NSThread currentThread]);

        }];

        //2.追加操作,追加的操作在子线程中执行

        [operation addExecutionBlock:^{

            NSLog(@"---download2--%@",[NSThread currentThread]);

        }];

        [operation addExecutionBlock:^{

             NSLog(@"---download3--%@",[NSThread currentThread]);

        }];

        //3.启动执行操作

        [operation start];

    ----------------------------------------------

    // 03 自定义NSOperation

        //如何封装操作?

        //自定义的NSOperation,    -(void)main

        {

            NSLog(@"--main--%@",[NSThread currentThread]);

        }

        //如何使用?

        //1.实例化一个自定义操作对象

        CJOperation *op = [[CJOperation alloc]init];

        //2.执行操作

        [op start];

    2.2 NSOperationQueue基本使用

    (1)NSOperation中的两种队列

    01 主队列 通过mainQueue获得,凡是放到主队列中的任务都将在主线程执行

    02 非主队列 直接alloc init出来的队列。非主队列同时具备了并发和串行的功能,通过设置最大并发数属性来控制任务是并发执行还是串行执行

    (2)相关代码

    //自定义NSOperation

    -(void)customOperation

    {

        //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.封装操作

        //好处:1.信息隐蔽

        //2.代码复用

        CJOperation *op1 = [[CJOperation alloc]init];

        CJOperation *op2 = [[CJOperation alloc]init];

        //3.添加操作到队列中

        [queue addOperation:op1];

        [queue addOperation:op2];

    }

    //NSBlockOperation

    - (void)block

    {

        //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.封装操作

        NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{

            NSLog(@"1----%@",[NSThread currentThread]);

        }];

        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{

            NSLog(@"2----%@",[NSThread currentThread]);

        }];

              

              //追加操作

        //如果操作里面的任务数量大于一,那么会开子线程一起执行

        [op2 addExecutionBlock:^{

            NSLog(@"3----%@",[NSThread currentThread]);

        }];

        [op2 addExecutionBlock:^{

            NSLog(@"4----%@",[NSThread currentThread]);

        }];

        //3.添加操作到队列中

        [queue addOperation:op1];

        [queue addOperation:op2];

        //补充:简便方法

        [queue addOperationWithBlock:^{

            NSLog(@"5----%@",[NSThread currentThread]);

        }];

    }

    //NSInvocationOperation

    - (void)invocation

    {

        /*

         GCD中的队列:

         串行队列:自己创建的,主队列

         并发队列:自己创建的,全局并发队列

         NSOperationQueue

         主队列:[NSOperationQueue mainqueue];凡事放在主队列中的操作都在主线程中执行

         非主队列:[[NSOperationQueue alloc]init],并发和串行,默认是并发执行的

         */

        //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.封装操作

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

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

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

        //3.把封装好的操作添加到队列中

        [queue addOperation:op1];//[op1 start]

        [queue addOperation:op2];

        [queue addOperation:op3];

    }

    2.3 NSOperation其它用法

    (1)设置最大并发数【控制任务并发和串行】

    //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.设置最大并发数

        //注意点:该属性需要在任务添加到队列中之前进行设置

    //该属性控制队列是串行执行还是并发执行

        //如果最大并发数等于1,那么该队列是串行的,如果大于1那么是并行的

        //系统的最大并发数有个默认的值,为-1,如果该属性设置为0,那么不会执行任何任务

        queue.maxConcurrentOperationCount = 2;

    (2)暂停和恢复以及取消

        //设置暂停和恢复

        //suspended设置为YES表示暂停,suspended设置为NO表示恢复

        //暂停表示不继续执行队列中的下一个任务,暂停操作是可以恢复的

        if (self.queue.isSuspended) {

            self.queue.suspended = NO;

        }else

        {

            self.queue.suspended = YES;

        }

        //取消队列里面的所有操作

        //取消之后,当前正在执行的操作的下一个操作将不再执行,而且永远都不在执行,就像后面的所有任务都从队列里面移除了一样

        //取消操作是不可以恢复的

        [self.queue cancelAllOperations];

    ---------自定义NSOperation取消操作--------------------------

    -(void)main

    {

        //耗时操作1

        for (int i = 0; i<1000; i++) {

            NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);

        }

        NSLog(@"+++++++++++++++++++++++++++++++++");

        //苹果官方建议,每当执行完一次耗时操作之后,就查看一下当前队列是否为取消状态,如果是,那么就直接退出

        //好处是可以提高程序的性能

        if (self.isCancelled) {

            return;

        }

        //耗时操作2

        for (int i = 0; i<1000; i++) {

            NSLog(@"任务1-%d--%@",i,[NSThread currentThread]);

        }

        NSLog(@"+++++++++++++++++++++++++++++++++");

    }

    2.4 NSOperation实现线程间通信

    (1)开子线程下载图片

     //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.使用简便方法封装操作并添加到队列中

        [queue addOperationWithBlock:^{

            //3.在该block中下载图片

            NSURL *url = [NSURL URLWithString:@"http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];

            NSData *data = [NSData dataWithContentsOfURL:url];

            UIImage *image = [UIImage imageWithData:data];

            NSLog(@"下载图片操作--%@",[NSThread currentThread]);

            //4.回到主线程刷新UI

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                self.imageView.image = image;

                NSLog(@"刷新UI操作---%@",[NSThread currentThread]);

            }];

        }];

    (2)下载多张图片合成综合案例(设置操作依赖)

    //02 综合案例

    - (void)download2

    {

        NSLog(@"----");

        //1.创建队列

        NSOperationQueue *queue = [[NSOperationQueue alloc]init];

        //2.封装操作下载图片1

        NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{

            NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/6a63f6246b600c3320b14bb3184c510fd8f9a185.jpg"];

            NSData *data = [NSData dataWithContentsOfURL:url];

            //拿到图片数据

            self.image1 = [UIImage imageWithData:data];

        }];

        //3.封装操作下载图片2

        NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{

            NSURL *url = [NSURL URLWithString:@"http://pic.58pic.com/58pic/13/87/82/27Q58PICYje_1024.jpg"];

            NSData *data = [NSData dataWithContentsOfURL:url];

            //拿到图片数据

            self.image2 = [UIImage imageWithData:data];

        }];

        //4.合成图片

        NSBlockOperation *combine = [NSBlockOperation blockOperationWithBlock:^{

            //4.1 开启图形上下文

            UIGraphicsBeginImageContext(CGSizeMake(200, 200));

            //4.2 画image1

            [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];

            //4.3 画image2

            [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];

            //4.4 根据图形上下文拿到图片数据

            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    //        NSLog(@"%@",image);

            //4.5 关闭图形上下文

            UIGraphicsEndImageContext();

            //7.回到主线程刷新UI

            [[NSOperationQueue mainQueue]addOperationWithBlock:^{

                self.imageView.image = image;

                NSLog(@"刷新UI---%@",[NSThread currentThread]);

            }];

        }];

        //5.设置操作依赖

        [combine addDependency:op1];

        [combine addDependency:op2];

        //6.添加操作到队列中执行

        [queue addOperation:op1];

        [queue addOperation:op2];

        [queue addOperation:combine];

        }

  • 相关阅读:
    SpringMVC学习笔记----常用注解
    python常用模块学习1
    python基础模块,包
    python-验证功能的装饰器示例
    python闭包及装饰器
    关于windows服务器配置
    python高阶函数
    python-生成器和迭代器
    linux--基础知识5
    python基础-文件操作的其他方法
  • 原文地址:https://www.cnblogs.com/chenjianjian/p/5406813.html
Copyright © 2020-2023  润新知