2016 - 1 -19 初探NSOperation

一:简介

1.NSOperation的作用:

配合NSOperation与NSOperationQueue也可以实现多线程。

2.NSOperation与NSOperationQueue实现多线程的步骤:

2.1现将需要执行的操作封装到NSOperation对象中

2.2然后将NSOperation添加到NSOperationQueue中

2.3系统会自动将NSOPerationQueue中的NSOPeration取出来

二:NSOPeration的子类

1.NSOPeration是个抽象类,并不具备封装操作的能力,必须使用他的子类。

2.使用NSOPeration子类的方式有三种:

2.1NSInvocationOpertation

2.2NSBlockOperating

2.3自定义子类继承NSOperation,实现内部相应方法

三:NSOPerationQueue

1.NSOperationQueue 的类型:

1.1主队列,通过 [NSOperationQueue mainQueue];可以获得。

1.2其他队列:串行队列,并发队列。 通过 [[NSOperationQueue alloc ] init];可以获得。

2.使用:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   NSOperationQueue *opQ = [[NSOperationQueue alloc ] init];

    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run1) object:nil];

    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run2) object:nil];

    [opQ addOperation:op1];// 会在内部调用[op1 start]这个方法并放入子线程内

    [opQ addOperation:op2];
}
- (void)run1{
    NSLog(@"1 ------ %@",[NSThread currentThread]);
}
- (void)run2{
    NSLog(@"2  ------ %@",[NSThread currentThread]);
}

3.自定义Operation

3.1使用场景:当操作比较复杂时,就可以自定义一个NSOperation,然后将复杂的操作放在自定义operation里。

3.2实现:

3.2.1需要在自定义Operation中重写main函数:

如:

- (void)main
{
    NSLog(@"%@-----------------自定义Operation",[NSThread currentThread]);
}

3.2.2创建自定义Operation 添加到队列里去:

ZZNSOpertation *op3 = [[ZZNSOpertation alloc] init];

[opQ addOperation:op3];

3.2.3打印结果:

2016-01-19 17:43:39.941 NSPerationQueue[16438:1477860] <NSThread: 0x7ff00a60d600>{number = 4, name = (null)}-----------------自定义Operation

时间: 2024-08-24 06:31:35

2016 - 1 -19 初探NSOperation的相关文章

Murano Weekly Meeting 2016.07.19

Meeting time: 2016.July.19 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1.Backports Link:  https://etherpad.openstack.org/p/murano-stable-backports/ 2.Convergence so both of our CI servers are running heat with convergence n

2016.1.19 DEV Express控件GirdControl使用

DEV控件在工具箱中消失处理方法 开始-->程序-->Developer Express v2009 vol 3(依据版本不同)-->Components-->Tools-->ToolboxCreator   1.点击一行选择完整一行 Run Designer->View->OptionsBehavior->EditorShowMode 设置为:Click Run Designer->View->OptionsSelection.EnableAp

翻译:Gregory Larsen,2016/02/19(第一版:2014年12月17日)高级T-SQL阶梯1级:使用CROSS JOIN介绍高级T-SQL

原文链接:http://www.sqlservercentral.com/articles/Stairway+Series/119933/ 原文作者:Gregory Larsen,2016/02/19(第一版:2014年12月17日) 系列 本文是"Stairway Series:Stairway to Advanced T-SQL"的一部分 这个阶梯将包含一系列文章,这些文章将在前面两个T-SQL阶梯,T-SQL DML和T-SQL超越基础知识的T-SQL基础上进行扩展. 这个楼梯应

2016.8.19

2016年8月19日.在杭州整整两个月.最近新添加了几个前端群.有一个群感觉特别的靠谱.很多分享.很多技术.给我的感觉有种干海绵碰到了大量的水.可能是做技术的对技术方面的渴求的这种感觉.老实说.之前对自己的技术是真的自信到不行.但是慢慢的,一些经历和经验.让自己对自己的判断越来越不像之前的那种浮夸感了.毕竟自己的经验还不够充足.资历就摆在这里.今天想到一句话.坐井观天,焉知是天? 勉励一下自己.希望能够不断前进.在努力和学习中不断攀高. 最后送一句:人生之所以迷茫,是因为我们的才华配不上我们的梦

2016/09/19

1. Python视频 1) 多层装饰器 USER_INFO = {} def check_login(func): def inner(*args, **kwargs): if USER_INFO.get('is_login', None): ret = func(*args, **kwargs) return ret else: print('please login') return inner def check_admin(func): def inner(*args, **kwarg

G_S男女匹配算法(算法的第一个程序2016.09.19)

1 #include<iostream> 2 using namespace std; 3 int main(){ 4 int Smallest_numberFree = 0;//记录单身的号码最低的男性 5 int i = Smallest_numberFree, n, k = -1;//n代表有多少对男女 6 7 cout << "请输出有多少对男女:"; 8 cin >> n; 9 int **Man = new int*[n], **Woma

2016.5.19——Excel Sheet Column Title

Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[i]-'A'+1.(为什么有+1,-1还有点迷糊,貌似是十进制是0-9,26进制是) 2.十进制到26进制转化 题目: Given a positive integer, return its corresponding column title as appear in an Excel shee

2016.9.19小程序

制作一个界面完成员工信息的添加.显示和查找 实现步骤: 1.制作界面 2.建立员工类,包含姓名.年龄.性别.电话4个私有属性,加上get和set方法作为访问器和修改器 3.在事件类中加入一个员工数组的属性,用于保存员工信息,加入一个number的属性,用于记录员工的人数. 4.当单击添加时,从四个文本框中得到信息,并封装成员工对象,然后将员工对象加入员工数组,人数加1.如果超出数组的长度,添加按钮变灰,阻止用户继续添加. 5.当单击显示时,弹出对话框,显示现有员工的信息. 6.在姓名文本框中输入

2016.01.19 UITextField

1.placeholder(占位符,就是默认提示文本) eg:_loginTextField.placeholder = @"QQ号/手机号/邮箱"; 2.borderStyle(设置边框类型) eg:_loginTextField.borderStyle = UITextBorderStyleRoundedRect; 各种类型: typedef NS_ENUM(NSInteger, UITextBorderStyle) { UITextBorderStyleNone, UITextB