zoukankan      html  css  js  c++  java
  • iOS中的多线程 NSOperation

      在ios中,使用多线程有三种方式,分别是:NSThread、NSOperation和NSOperationQueue、GCD,在本节,主要讲解一下NSOperation的使用。

      NSOperation和NSOperationQueue这种方式实际上是将NSOperation的对象放到一个NSOperationQueue队列中,然后依次启动操作,类似于线程池的使用。

      在使用的过程中,NSOperation的操作使用的是它的子类,分别是NSInvocationOperation和NSBlockOperation,两者没有本质的区别,只不过后者以Block的方式来实现,使用相对简单。NSOperationQueue主要负责管理和执行所有的NSOperation对象,并控制线程之间的执行顺序与依赖关系。

      下面,通过NSOperation开始多线程从网络获取图片并刷新。

    NSInvocationOperation

    代码

    //  ViewController.m
    //  AAAAAA
    //
    //  Created by jerei on 15-11-8.
    //  Copyright (c) 2015年 jerehedu. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    #pragma mark - 点击按钮开启线程下载图片
    - (IBAction)click_InvocationOpreation_load:(UIButton *)sender {
        
        NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"];
        
        //创建一个operation
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(loadImageWithUrl:) object:url];
    
        //添加到操作队列中
        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [queue addOperation:operation];
    }
    
    #pragma mark - 根据url获取图片
    -(void)loadImageWithUrl:(NSURL *)url{
    
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        //回到主线程更新界面
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(updateImageView:) object:image];
        
        [[NSOperationQueue mainQueue] addOperation:operation];
    }
    
    #pragma mark - 更新界面
    -(void)updateImageView:(UIImage *)img{
        _imageView.image = img;
    }
    
    @end

    NSBlockOperation

    代码

    //  ViewController.m
    //  AAAAAA
    //
    //  Created by jerei on 15-11-8.
    //  Copyright (c) 2015年 jerehedu. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    #pragma mark - 点击按钮开启线程下载图片
    - (IBAction)click_BlockOpreation_load:(UIButton *)sender {
        
        //创建操作队列
        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
        //设置最大并发线程数
        operationQueue.maxConcurrentOperationCount = 5;
        
        //<方法一> 创建operation
    //    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
    //        //根据url请求数据
    //        NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"];
    //        [self loadImageWithUrl:url];
    //    }];
    //    
    //    //添加到队列中
    //    [operationQueue addOperation:operation];
        
        //<方法二> 创建operation
        [operationQueue addOperationWithBlock:^{
            //根据url请求数据
            NSURL *url = [NSURL URLWithString:@"http://www.jerehedu.com/images/temp/logo.gif"];
            [self loadImageWithUrl:url];
        }];
    }
    
    #pragma mark - 根据url获取图片
    -(void)loadImageWithUrl:(NSURL *)url{
    
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        
        //回到主线程更新界面
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [self updateImageView:image];
        }];
    }
    
    #pragma mark - 更新界面
    -(void)updateImageView:(UIImage *)img{
        _imageView.image = img;
    }
    
    @end
    作者:杰瑞教育
    出处:http://www.cnblogs.com/jerehedu/ 
    版权声明:本文版权归杰瑞教育技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    技术咨询:JRedu技术交流
     
  • 相关阅读:
    动态规划解按摩师的最长预约时间
    C#中WinForm的Tab键顺序调整顺序
    内网穿透工具对比FRP+NPS+Zerotier与NAT服务器测试
    " " 和 ' ' 混用拼接html字符串,且含有事件,事件中有参数
    HAProxy在Windows下实现负载均衡与反向代理
    react 导入src外部的文件 Relative imports outside of src/ are not supported.
    11_实例
    C#删除指定目录下文件(保留指定几天前的日志文件)
    【转】系统创建定时执行任务bat批处理删除指定N天前文件夹的文件
    mariadb导如数据异常------Error Code: 1153
  • 原文地址:https://www.cnblogs.com/jerehedu/p/5141074.html
Copyright © 2011-2022 走看看