IOS NSOperation&NSOperationQueue
//
// ViewController.m
// CX-NSOperationQueue
//
// Created by ma c on 16/3/15.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// /*
GCD -- (iOS4.0)多线程解决方案
将任务(block)添加到队列(串行,并行(全局队列)),指定执行任务的方法。(同步,异步)
拿到dispatch_get_main_queue,线程之间的通信 NSOperation -- (iOS2.0) (后经苹果改造)
将操作添加到队列里
[NSOperationQueue mainQueue] 获取主队列,将任务添加到主队列,就会在主线程中执行
NSOperation可以设置最大并发数(用GCD处理,比较麻烦)
可以暂停可以继续,也就是挂起操作
取消所有的任务
设置依赖关系
*/ #import "ViewController.h" @interface ViewController ()
//NSOperation 队列
@property (nonatomic, strong) NSOperationQueue * queue; @end @implementation ViewController -(NSOperationQueue *)queue{ if (!_queue) {
_queue = [[NSOperationQueue alloc]init];
}
return _queue;
} - (void)viewDidLoad {
[super viewDidLoad];
//线程间通信
// [self contact];
//最大并发数 注意观察测试时间
[self maxCount]; //常用操作
/*
取消操作:- (void)cancelAllOperations;
挂起操作:@property (getter=isSuspended) BOOL suspended;
依赖关系:- (void)addDependency:(NSOperation *)op;
取消依赖:- (void)removeDependency:(NSOperation *)op;
*/ } -(void)maxCount{ self.queue.maxConcurrentOperationCount = ; for (NSInteger i = ; i < ; i ++) { NSInvocationOperation * operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(test) object:nil]; [self.queue addOperation:operation];
}
}
-(void)test{
[NSThread sleepForTimeInterval:];
NSLog(@"%@",[NSThread currentThread]);
}
//线程间通信
-(void) contact{ [self.queue addOperationWithBlock:^{ NSLog(@"%@",[NSThread currentThread]);
NSLog(@"One"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"Two");
}];
}];
}
- (void)test3 { //NSOperation是基于GCD的,把GCD的block封装成opertion,NSOperationQueue是全局队列封装
//将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步执行的。 //创建一个操作队列
NSOperationQueue * queue = [[NSOperationQueue alloc] init]; NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil]; NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"op2 - %@",[NSThread currentThread]);
}]; //添加NSOperation到NSOperationQueue中
//NSOperation添加到queue之后,通常短时间内就会得到运行。
// [queue addOperation:op];
// [queue addOperation:op2]; // waitUntilFinished yes 操作完成后执行下面的代码 no 先执行下面的代码 //添加一个block形式的operation
[queue addOperationWithBlock:^{ NSLog(@"op3 - %@",[NSThread currentThread]); }]; [queue addOperations:@[op,op2] waitUntilFinished:NO]; // NSLog(@"完成"); } - (void)test2 { // 能够并发地执行一个或多个block对象,所有相关的block都执行完之后,操作才算完成
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"%@",[NSThread currentThread]); NSLog(@"第一个操作");
}]; // 通过addExecutionBlock方法添加block操作,开启多个线程
[op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第二个操作");
}]; [op addExecutionBlock:^{ NSLog(@"%@",[NSThread currentThread]); NSLog(@"第三个操作");
}]; [op start];
} - (void)test1 { // 基于一个对象和selector来创建操作。如果你已经有现有的方法来执行需要的任务,就可以使用这个类 NSInvocationOperation * op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test) object:nil]; // 如果我们想在一个NSOperation执行完毕后做一些事情,就调用NSOperation的setCompletionBlock方法来设置想做的事情
[op setCompletionBlock:^{
NSLog(@"完成");
}]; // 开始执行任务(同步执行)
// 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的。
[op start]; }
@end
*有关进程与线程我会进行总结*
*由于昨天网络出现问题,昨晚把代码写出来了,但是没有上传
IOS NSOperation&NSOperationQueue的更多相关文章
- iOS中的多线程NSThread/GCD/NSOperation & NSOperationQueue
iOS多线程有四套多线程方案: Pthreads NSThread GCD NSOperation & NSOperationQueue 接下来我来一个一个介绍他们 Pthreads 在类Un ...
- IOS多线程(NSOperation,NSOperationQueue)
含义:NSOperation,NSOperationQueue是什么. The NSOperation class is an abstract class you use to encapsulat ...
- iOS NSOperation
iOS NSOperation 一.简介 除了,NSThread和GCD实现多线程,配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOp ...
- iOS开发之NSOperation & NSOperationQueue
1.简介 (1) NSOperationQueue(操作队列)是由GCD提供的队列模型的Cocoa抽象,是一套Objective-C的API,为了使并发(多线程)编程变得更加简单,但效率比GCD略低. ...
- iOS NSOperation的使用
先给出NSOpetation的官方指导https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation ...
- iOS -NSOperation并发编程
http://www.cocoachina.com/game/20151201/14517.html http://blog.csdn.net/qinlicang/article/details/42 ...
- iOS NSOperation 封装 通知实现界面更新
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MYOperation : NSOpe ...
- iOS NSOperation 异步加载图片 封装NSOperation 代理更新
#import <Foundation/Foundation.h> @class MYOperation; @protocol MYOperationDelecate <NSObje ...
- iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别
In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central D ...
随机推荐
- CentOS下安装JDK7 转载
转载地址:http://www.cnblogs.com/rilley/archive/2012/02/02/2335395.html CentOS下安装JDK7 下载地址:http://www.ora ...
- 2016 SDCC会后总结
很荣幸作为前端专题讲师参加2016年SDCC,与周爱民老师同台,听业界牛人的分享真是受益匪浅.对我来说是第一次在如此规模的专题论坛上演讲,全程紧张的要命,提前准备好的内容有很多因为紧张没有讲出来.此次 ...
- C#语法糖之Cookies操作类 asp.net
用法: //声名一个数据集合 var listString = new List<string>() { "a", "b", "c&quo ...
- HTML解析类 ,让你不使用正则也能轻松获取HTML相关元素 -C# .NET
功能: 1.轻松获取指元素HTML元素. 2.可以根据属性标签进行筛选 3.返回的都是Llist强类型无需转换 用过XElement的都知道 用来解析XML非常的方便,但是对于HTML的格式多样化实在 ...
- ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法
方法一:通过Request.Form [HttpPost] public ActionResult Test() { string id=Reques ...
- chrome开发者工具浅析--timeline
一.概述 ...
- mysql创建每月执行一次的event
DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ EVENT `dev_sp2p6`.`e_test1` ON SCHEDULEE ...
- [Tool] 源代码管理之Git
本节目录 什么是Git 什么是GitHub 安装Git GitHub之Repository GitHub之托管页面 常用Git 命令 什么是Git 由于现在的开发多人协同办公,因此出现源代码管理工具 ...
- GoodsAndStaffManagermentSystem----Sprint 计划1
需求分析:茗仕茶业管理系统是个商品-员工综合管理系统. 员工管理:设置不同的职位,不同职位有不同访问权限.员工只能卖商品,财务员只有查看商品的库存量和商品销售记录:公司老板可以查看所有信息-- ...
- P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1
P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1 May ...