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. NSnotificationCenter 正确使用姿势, removeObject 探索

    最近在做平板的过程中,发现了一些很不规范的代码.偶然修复支付bug的时候,看到其他项目代码,使用通知的地方没有移除,我以为我这个模块的支付闪退是因为他通知没有移除的缘故.而在debug和看了具体的代码 ...

  2. BZOJ 1026: [SCOI2009]windy数( dp )

    dp..dp(x, t) 表示共x位, 第x位为t有多少个windy数. 对答案差分, 我们只需统计1 ~ l-1和1 ~ r的windy数数量. 考虑如何计算[1, n]的答案 : 从最高位到最低位 ...

  3. junit4笔记

    这两天在复习hibernate,看的小峰的视频,觉得很不错. 现在把里面的junit4的一些使用方法记下来.方便以后的差用.代码如下. package com.java1234.service; im ...

  4. 浙江工商大学15年校赛E题 无邪的飞行棋 【经典背包】

    无邪的飞行棋 Time Limit 1s Memory Limit 64KB Judge Program Standard Ratio(Solve/Submit) 15.38%(4/26) Descr ...

  5. POJ1054 枚举【STL__binary_search()_的应用】

    ①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...

  6. JAVA的一些小笔记

    构造块优先于构造方法执行,而且每当有一个新的实例化对象产生时,就会重复执行构造块的程序. 静态块优先于构造块执行,而且不管有多少个实例化对象产生,静态块只会执行一次,它的主要作用是为类中的static ...

  7. 显示器中关于HS,VS,HBP,VBP参数浅析

    先来解释几个缩写的含义 HSYNC : 水平同步信号(horizontal synchronization signal) VSYNC :  垂直同步(Vertical Sync):场同步 FR : ...

  8. 用QFileSystemWatcher来监视文件和目录的改变(内部还是使用了timer)

    Use Case: 两个程序共享同一个Configuration文件,当一个程序作出改变的时候,需要另外一个程序能够及时响应. 之前其实猜的八九不离十,估计是有一个Timer,然后定时查询Config ...

  9. 运行计划之误区,为什么COST非常小,SQL却跑得非常慢?

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/38321477 2014.7.31就晚20:30 My Oracle Support组猫大师 ...

  10. UNIX网络编程——网络数据包检测

    网络数据包检测 数据包捕获(sniffer):是指在网络上进行数据收集的行为,需要通过网卡来完成. 三种访问方式: BSD Packet Filter(BPF) SVR4 Datalink Provi ...