首页 技术 正文
技术 2022年11月14日
0 收藏 348 点赞 3,547 浏览 2522 个字

介绍:

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

实现步骤

  第1步:将一个操作封装到NSOperation对象中

  第2步:将NSOperation对象放入NSOperationQueue队列

  第3步:NSOperationQueue自动取出队列中的NSOperation对象放到一条线程中执行

具体实现

  在swift中的实现方式分2种(oc还多了一个NSInvocationOperation,并且在oc中NSOperation是个抽象类):

  1.NSBlockOperation

  2.自定义子类继承NSOperation

目录:

  1. NSOperation常用操作
  2. NSOperation操作依赖
  3. NSOperation操作监听
  4. NSOperation线程通信
  5. 注意

1.NSOoperation常用操作,创建队列,设置最大并发数。

//创建队列
let queue = NSOperationQueue()
//设置最大并发数
queue.maxConcurrentOperationCount= //创建operation
let operation = NSBlockOperation { () -> Void in
print("doSomething1 \(NSThread.currentThread())")
} //当operation有多个任务的时候会自动分配多个线程并发执行,
//如果只有一个任务,会自动在主线程同步执行
//operation.start() operation.addExecutionBlock { () -> Void in
print("doSomething2 \(NSThread.currentThread())")
} operation.addExecutionBlock { () -> Void in
print("doSomething3 \(NSThread.currentThread())")
} let operation2=NSBlockOperation { () -> Void in
print("doSomething4 \(NSThread.currentThread())")
} //添加到队列中的operation将自动异步执行
queue.addOperation(operation)
queue.addOperation(operation2) //还有一种方式,直接将operation的blcok直接加入到队列
queue.addOperationWithBlock { () -> Void in
print("doSomething5 block \(NSThread.currentThread())")
}
queue.addOperationWithBlock { () -> Void in
print("doSomething6 block \(NSThread.currentThread())")
}
queue.addOperationWithBlock { () -> Void in
print("doSomething7 block \(NSThread.currentThread())")
}
queue.addOperationWithBlock { () -> Void in
print("doSomething8 block \(NSThread.currentThread())")
}

2.NSOperation操作依赖,可设置一个操作在另一个操作完成后在执行

 //创建队列
let queue = NSOperationQueue() let operationA = NSBlockOperation { () -> Void in
print("print A")
}
let operationB = NSBlockOperation { () -> Void in
print("print B")
}
let operationC = NSBlockOperation { () -> Void in
print("print C")
} //B等A执行完才执行
operationB.addDependency(operationA)
//C等B执行完才执行
operationC.addDependency(operationB) queue.addOperation(operationA)
queue.addOperation(operationB)
queue.addOperation(operationC)

3.NSOperation操作监听,一个操作完成后调用另一个操作:

   func operationCompletion(){
//创建队列
let queue = NSOperationQueue()
let operation = NSBlockOperation { () -> Void in
print("print A")
}
operation.completionBlock = doSomething
queue.addOperation(operation)
}
func doSomething(){
print("doSomething")
}

4.NSOperation线程通信,NSOperationQueue.mainQueue。

    //创建队列
let queue = NSOperationQueue()
queue.addOperationWithBlock { () -> Void in
print("子线程 \(NSThread.currentThread())")
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
print("主线程 \(NSThread.currentThread())")
})
}

注意

  1.在使用队列任务的时候,内存警告的时候可使用队列的cancelAllOperations函数取消所有操作,注意一旦取消不可恢复。亦可设置队列的suspended属性暂停和恢复队列。

  2.在设置操作依赖的时候不能设置循环依赖。

完!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849