iOS多线程应用开发中使用NSOperation类的基本方法
一、NSOperation简介
1.简单说明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
(1)先将需要执行的操作封装到一个NSOperation对象中
(2)然后将NSOperation对象添加到NSOperationQueue中
(3)系统会⾃动将NSOperationQueue中的NSOperation取出来
(4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法
二、具体说明
1.NSInvocationOperation子类
创建对象和执行操作:
//创建操作对象,封装要执行的任务 //NSInvocationOperation 封装操作 NSInvocationOperation*operation=[[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(test)object:nil]; //执行操作 [operationstart];
说明:一旦执⾏操作,就会调用target的test方法
代码示例:
// // YYViewController.m // 01-NSOperation基本1 // // Createdby孔医己on14-6-25. // Copyright(c)2014年itcast.Allrightsreserved. //
#import"YYViewController.h"
@interfaceYYViewController()
@end
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下