Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术:
(1)NSThread
(2)Cocoa NSOperation(NSOperation和NSOperationQueue)
(3)Grand Central Dispath(GCD)
Cocoa
NSOperation不需要关心线程管理和数据同步的事情,可以把精力放在自己需要执行的操作上。相关的类有NSOperation和
NSOperationQueue。其中NSOperation是个抽象类,使用它必须用它的子类,可以实现它或者使用它定义好的子
类:NSBlockOperation。创建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
27
28
29
|
import UIKitclass 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 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
|
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 UIKitclass 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 data = NSData(contentsOfURL: NSURL(string: imageUrl)!, options: nil, error: nil) println(data?.length) }} |
4,设置运行队列并发数
|
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 = completionBlockdispatch_after(dispatch_time(DISPATCH_TIME_NOW, 4), dispatch_get_main_queue(), { println("Complete")}) |
Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue的更多相关文章
- Swift - 多线程实现方式(1) - NSThread
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- iOS 多线程 NSOperation、NSOperationQueue
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...
- 『NSOperation、NSOperationQueue』详解
1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperationQueue 是苹果提供给我们的一套多线程解决方案.实际上 NSOperation.N ...
- iOS开发:Swift多线程NSOperation的使用
介绍: NSOperation是基于GCD实现,封装了一些更为简单实用的功能,因为GCD的线程生命周期是自动管理,所以NSOperation也是自动管理.NSOperation配合NSOperatio ...
- 用NSOperation和NSOperationQueue实现多线程编程
1.上一讲简单介绍了NSThread的使用,虽然也可以实现多线程编程,但是需要我们去管理线程的生命周期,还要考虑线程同步.加锁问题,造成一些性能上的开销.我们也可以配合使用NSOperation和NS ...
- [转] iOS多线程编程之NSOperation和NSOperationQueue的使用
<iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是用定义好 ...
- iOS-----使用NSOperation与NSOperationQueue实现多线程
使用NSOperation与NSOperationQueue实现多线程 NSOperation与NSOperationQueue的基本理论如下. NSOperationQueue 代表一个FIFO的队 ...
- iOS多线程编程之NSOperation和NSOperationQueue的使用
前一篇 <iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是 ...
随机推荐
- 【IE】trim()方法失效
今天用了$.ajax异步提交,结果在ie8里面报错了,说不支持此对象,找了半天没找到什么问题. 后来发现是我data里面的参数传递,里面有个参数用到了trim()方法,这个方法在ie8里面是失效的. ...
- 【JavaScript】标签样式中多出了element.style
是因为使用了js增加或修改了样式.
- A - Prime Ring Problem(素数环,深搜,打表)
Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into ...
- 字符设备驱动4: ioremap
#define GPIO_OFT(x) ((x) - 0x56000000)#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT ...
- BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )
题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...
- A valid provisioning profile for this executable was not found.
- 显示器中关于HS,VS,HBP,VBP参数浅析
先来解释几个缩写的含义 HSYNC : 水平同步信号(horizontal synchronization signal) VSYNC : 垂直同步(Vertical Sync):场同步 FR : ...
- docker学习笔记7:发布镜像到docker hub上
镜像创建好后,很重要的一个操作就是共享和发布.可以将自己创建的镜像发布到docker hub上,也可以发布到自己的私有docker hub上. 要想发布镜像到dokcer hub上,首先要在dokce ...
- vs2010断点使用技巧
msdn:http://msdn.microsoft.com/zh-cn/library/vstudio/5557y8b4.aspx 有很多断点的文章. 为提供更大的灵活性,Visual Studio ...
- BZOJ 3505: [Cqoi2014]数三角形( 组合数 )
先n++, m++ 显然答案就是C(3, n*m) - m*C(3, n) - n*C(3, m) - cnt. 表示在全部点中选出3个的方案减去不合法的, 同一行/列的不合法方案很好求, 对角线的不 ...