NSOperation
自定义operation
相比GCD,可以中断任务,也可使用 addDependency,对要执行的任务进行排序..
//
// CustomOperation.h
// Test
//
// Created by M on 16/1/5.
// Copyright © 2016年 Meng. All rights reserved.
// #import <Foundation/Foundation.h> @protocol CustomOperationDelegate <NSObject> -(void)handleDelegate:(NSString*)str; @end @interface CustomOperation : NSOperation @property(nonatomic,assign)id<CustomOperationDelegate> Delegate;
@property(nonatomic)BOOL IsStopOperation; -(id)initWithOperationDelegate:(id<CustomOperationDelegate>) delegate; @end
//
// CustomOperation.m
// Test
//
// Created by M on 16/1/5.
// Copyright © 2016年 Meng. All rights reserved.
// #import "CustomOperation.h" @implementation CustomOperation -(id)initWithOperationDelegate:(id<CustomOperationDelegate>)delegate
{ if (self = [super init]) { self.Delegate = delegate; } return self; } -(void)main
{
@autoreleasepool { if ([self.Delegate respondsToSelector:@selector(handleDelegate:)]) { for (int i = ; i<; i++) { //中断operation
if (self.IsStopOperation) {
return;
} sleep(); [self.Delegate performSelector:@selector(handleDelegate:) withObject:[NSString stringWithFormat:@"CustomOperation,%d",i]]; }
} }
} @end
ViewController实现
//
// ViewController.m
// Test
//
// Created by M on 16/1/4.
// Copyright © 2016年 Meng. All rights reserved.
// #import "ViewController.h"
#import "CustomOperation.h" @interface ViewController ()<CustomOperationDelegate>//实现委托
{
CustomOperation *CO;
UILabel *CountLbl;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self setupView]; NSOperationQueue *qu = [[NSOperationQueue alloc] init]; CO = [[CustomOperation alloc] initWithOperationDelegate:self]; [qu addOperation:CO];//添加到Queue #pragma mark ======================= NSBlockOperation *BKO1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"BKO1,%@", [NSThread currentThread]); for (int i = ; i<; i++) {
NSLog(@"111111:%d",i);
sleep();
}
}]; NSBlockOperation *BKO2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"BKO2,%@", [NSThread currentThread]); for (int i = ; i<; i++) {
NSLog(@"222222:%d",i);
sleep();
}
}]; // BKO2 依赖 BKO1
[BKO2 addDependency:BKO1]; [qu addOperation:BKO1];
[qu addOperation:BKO2]; #pragma mark ======================= NSOperationQueue *qu2 = [[NSOperationQueue alloc] init];
//把任务队列的最大并发数设置为1,也可以进行执行的排序....只不过是否有其他问题,还请指教.
qu2.maxConcurrentOperationCount = ; for (int i = ; i < ; i ++) { [qu2 addOperation:[NSBlockOperation blockOperationWithBlock:^{
sleep();
NSLog(@"BKO:%d",i);
}]]; } } -(void)setupView
{ CountLbl = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height/, self.view.bounds.size.width/, )]; CountLbl.textAlignment = NSTextAlignmentCenter; [self.view addSubview:CountLbl]; UIButton *stopBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/, self.view.bounds.size.height/-, self.view.bounds.size.width/, )]; stopBtn.backgroundColor = [UIColor darkGrayColor]; [stopBtn setTitle:@"Stop" forState:UIControlStateNormal]; [self.view addSubview:stopBtn]; [stopBtn addTarget:self action:@selector(StopOperation) forControlEvents:UIControlEventTouchUpInside]; } -(void)StopOperation
{ CO.IsStopOperation = YES;
}
#pragma mark Delegate
-(void)handleDelegate:(NSString *)str
{
//update the label text dispatch_async(dispatch_get_main_queue(), ^{
CountLbl.text = str;
}); } - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

NSOperation的更多相关文章
- iOS多线程之9.自定义NSOperation
本文主要讲如何自定义NSOperation,以及自定义NSOperation的一些注意事项,以下载图片为例. 新建一个类,继承于NSOperation. CustomOperation.h 代码 ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
- iOS多线程之7.NSOperation的初识
NSOperation和GCD一样,不用我们管理线程的生命周期,加锁等问题,只要把操作封装进NSOperation中,系统会自动帮我们创建线程,执行操作.而且他是面向对象的,我们看起来更容易理解,使用 ...
- 4.4 多线程进阶篇<下>(NSOperation)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人"简书" 本文源码 Demo 详见 Github https://github.c ...
- 认识和使用NSOperation
原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...
- 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用
本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...
- 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 ...
- 3.多线程NSOperation
1.NSOperation的基本操作 使用NSOperation的两个子类,NSInvocationOperation 和 NSBlockOperation 创建操作,然后将操作添加到队列中去执行 / ...
- NSOperation的start与main,并发与非并发。
http://blog.csdn.net/a2331046/article/details/52294006 在ios4以前,只有非并发的情况下,队列会为operation开启一个线程来执行.如果是并 ...
随机推荐
- angularjs笔记(二)
AngularJS API 4.AngularJS过滤器 使用一个管道符(|)添加到表达式和指令中 例1.格式化字母转为大写 <!DOCTYPE html> <html> &l ...
- 15款优秀移动APP产品原型设计工具
一新来小盆友问:“移动产品原型设计都用啥工具?” 答:“@#¥……&%*” 又问:“能详细说下各个工具吗?我比较一下” “……” 好吧,谁让我那么的爱分享而你又是小美女呢 ———————正文开 ...
- 只是为了好玩——Linux之父林纳斯自传
http://yuedu.163.com/source/d227d1ce35d248b1a00471c11464d5d9_4
- asp.net(C#)页面事件顺序
asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html 1 using System.Data; ...
- 二叉排序树(Binary Sort Tree)
参考文章:http://blog.csdn.net/ns_code/article/details/19823463 不过博主的使用第一种方法操作后的树已经不是二叉排序树了,值得深思!! #inclu ...
- Media Wiki
https://www.mediawiki.org/wiki/Help:Images/zh https://www.mediawiki.org/wiki/Manual_talk:Image_admin ...
- commons-pool实战之 GenericObjectPool和GenericKeyedObjectPool
前面两篇文章说了怎么样简单的使用commons-pool库,这里需要考虑一个问题就是在很多时候我们在池里的对象都是比较重型的并且大多数比较稀缺的 资源,比如说数据库连接,这样如果一直把一些连接放在池里 ...
- ajax实例详解
页面通过ajax和后台进行数据交互是非常简洁且方便的.特别是封装成json数据格式. 此处使用的是jQuery的ajax var params = { version:new Date().getTi ...
- html 等页面防止中文出现乱码的终极解决方案
网页UTF-8中文乱码问题解决方法 网页UTF-8中文乱码问题解决方法只有经过多方面测试的东西才有质量的保证和说服力,之前一直都是在本地做开发,经过本地测试也是通过的,但一发布到远程服务器上就问题百出 ...
- 高效的使用 Response.Redirect
介绍: 我正在评估一个 ASP.NET Web 项目应用.它有一些可扩展性问题.意味着当网站访问量增加的时候.系统将会变得缓慢.当我查看应用日志.我找到了大量的 ThreadAbortExceptio ...