1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术:
(1)NSThread
(2)Cocoa NSOperation(NSOperation和NSOperationQueue)
(3)Grand Central Dispath(GCD)

2,本文着重介绍Cocoa NSOperation
Cocoa
NSOperation不需要关心线程管理和数据同步的事情,可以把精力放在自己需要执行的操作上。相关的类有NSOperation和
NSOperationQueue。其中NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的子
类:NSBlockOperation。创建NSOperation子类的对象,把对象添加到NSOperationQueue队列里执行。
3,使用NSOperation的两种方式
(1)直接用定义好的子类:NSBlockOperation。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
         
        var operation:NSBlockOperation = NSBlockOperation(block: { [weak self] in
            self?.downloadImage()
            return
        })
         
        //创建一个NSOperationQueue实例并添加operation
        var queue:NSOperationQueue = NSOperationQueue()
        queue.addOperation(operation)
    }
     
    //定义一个下载图片的方法,线程调用
    func downloadImage(){
        var imageUrl = "http://hangge.com/blog/images/logo.png"
        var data = NSData(contentsOfURL: NSURL(string: imageUrl)!, options: nil, error: nil)
        println(data?.length)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

(2)继承NSOperation

然后把NSOperation子类的对象放入NSOperationQueue队列中,一旦这个对象被加入到队列,队列就开始处理这个对象,直到这个对象的所有操作完成,然后它被队列释放。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import UIKit
 
class ViewController: UIViewController {
 
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //创建线程对象
        var downloadImageOperation:DownloadImageOperation = DownloadImageOperation()
         
        //创建一个NSOperationQueue实例并添加operation
        var queue:NSOperationQueue = NSOperationQueue()
        queue.addOperation(downloadImageOperation)
    }  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
 
class DownloadImageOperation: NSOperation {
    override func main(){
        var imageUrl = "http://hangge.com/blog/images/logo.png"
        var data = NSData(contentsOfURL: NSURL(string: imageUrl)!, options: nil, error: nil)
        println(data?.length)
    }
}

4,设置运行队列并发数

NSOperationQueue队列里可以加入很多个NSOperation,可以把NSOperationQueue看做一个线程池,可往线程池中添加操作(NSOperation)到队列中。
可以设置线程池中的线程数,也就是并发操作数。默认情况下是-1,-1表示没有限制,这样可以同时运行队列中的全部操作。
1
2
//设置并发数
queue.maxConcurrentOperationCount = 5

5,取消队列所有操作

1
2
//取消所有线程操作
queue.cancelAllOperations()

6,每个NSOperation完成都会有一个回调表示任务结束

1
2
3
4
5
6
7
//定义一个回调
var completionBlock:(() -> Void)?
//给operation设置回调
operation.completionBlock = completionBlock
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4), dispatch_get_main_queue(), {
    println("Complete")
})

Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue的更多相关文章

  1. Swift - 多线程实现方式(1) - NSThread

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

  2. Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

  3. iOS 多线程 NSOperation、NSOperationQueue

    1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...

  4. 『NSOperation、NSOperationQueue』详解

    1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...

  5. iOS开发:Swift多线程NSOperation的使用

    介绍: NSOperation是基于GCD实现,封装了一些更为简单实用的功能,因为GCD的线程生命周期是自动管理,所以NSOperation也是自动管理.NSOperation配合NSOperatio ...

  6. 用NSOperation和NSOperationQueue实现多线程编程

    1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation和NS ...

  7. [转] iOS多线程编程之NSOperation和NSOperationQueue的使用

    <iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是用定义好 ...

  8. iOS-----使用NSOperation与NSOperationQueue实现多线程

    使用NSOperation与NSOperationQueue实现多线程 NSOperation与NSOperationQueue的基本理论如下. NSOperationQueue 代表一个FIFO的队 ...

  9. iOS多线程编程之NSOperation和NSOperationQueue的使用

    前一篇 <iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是 ...

随机推荐

  1. PHP - 直接输出对象的版本问题

  2. PyconChina2015丁来强Pydata Ecosystem

    pydata ecosystem基于python的数据分析生态系统 0. Agenda Data Science ecosystem Data Wrangling Data Analysis Data ...

  3. Java -- sleep and wait

    1.二者的来源 sleep(),是Thread下面的静态方法/静态本地方法. wait(),是Object()的final方法. 2.源码分析 a.sleep() public static void ...

  4. Gauss elimination Template

    Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...

  5. .Net Core 环境搭建

    .Net Core 系列:1.环境搭建 前言: 2016年6月28日微软宣布发布 .NET Core 1.0.ASP.NET Core 1.0 和 Entity Framework Core 1.0. ...

  6. mojo 默认use utf8;

    my $endtime=strftime("%Y%m%d%H%M%S",localtime()); my $d=encode_utf8('验证'); if ($a3 =~/$d/) ...

  7. 基于visual Studio2013解决算法导论之016查找最大值最小值

     题目 查找最大.最小值 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  8. 给刚通过51入门的新人讲讲S12(MCS12XS128)与51的差别

    MCS51是keil也对应地做好了非常多,也就是有非常多对你而言是透明的,是你不必关心的,你所要接触的寄存器数量也非常小,在这个时候你很多其它是写函数,仅仅只是针对这个平台写C程序比在PC上写C控制台 ...

  9. 【转】CentOS上安装 jdk:rpm安装和源码安装

    1.安装 jdk-8u5-linux-x64.rpm 原文链接:http://www.cnblogs.com/xsi640/p/3756995.html 先下载最新的jdk版本 文件名:jdk-8u5 ...

  10. NSStringDrawingOptions

    如果options参数为NSStringDrawingUsesLineFragmentOrigin,那么整个文本将以每行组成的矩形为单位计算整个文本的尺寸.(在这里有点奇怪,因为字体高度大概是13.8 ...