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技术交流
     
  • 相关阅读:
    2020 浏览器市场份额
    Web高级 JavaScript中的算法
    Web高级 JavaScript中的数据结构
    JavaScript 集合对象
    Web高级 网站安全
    Web高级 HTTP报文
    Web高级 Eventloop和事件执行顺序
    Web高级 Ajax和跨域CORS
    javaScript 继承
    JavaScript 事件基础
  • 原文地址:https://www.cnblogs.com/jerehedu/p/5141074.html
Copyright © 2011-2022 走看看