自定义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的更多相关文章

  1. iOS多线程之9.自定义NSOperation

      本文主要讲如何自定义NSOperation,以及自定义NSOperation的一些注意事项,以下载图片为例. 新建一个类,继承于NSOperation. CustomOperation.h 代码 ...

  2. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  3. iOS多线程之7.NSOperation的初识

    NSOperation和GCD一样,不用我们管理线程的生命周期,加锁等问题,只要把操作封装进NSOperation中,系统会自动帮我们创建线程,执行操作.而且他是面向对象的,我们看起来更容易理解,使用 ...

  4. 4.4 多线程进阶篇<下>(NSOperation)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人"简书" 本文源码 Demo 详见 Github https://github.c ...

  5. 认识和使用NSOperation

    原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...

  6. 多线程下NSOperation、NSBlockOperation、NSInvocationOperation、NSOperationQueue的使用

    本篇文章主要介绍下多线程下NSOperation.NSBlockOperation.NSInvocationOperation.NSOperationQueue的使用,列举几个简单的例子. 默认情况下 ...

  7. iOS NSOperation 封装 通知实现界面更新

    #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface MYOperation : NSOpe ...

  8. iOS NSOperation 异步加载图片 封装NSOperation 代理更新

    #import <Foundation/Foundation.h> @class MYOperation; @protocol MYOperationDelecate <NSObje ...

  9. 3.多线程NSOperation

    1.NSOperation的基本操作 使用NSOperation的两个子类,NSInvocationOperation 和 NSBlockOperation 创建操作,然后将操作添加到队列中去执行 / ...

  10. NSOperation的start与main,并发与非并发。

    http://blog.csdn.net/a2331046/article/details/52294006 在ios4以前,只有非并发的情况下,队列会为operation开启一个线程来执行.如果是并 ...

随机推荐

  1. ubuntu下Eclipse下添加GBK编码

    把Windows下的工程导入到了Linux下Eclipse中,由于以前的工程代码,都是GBK编码的(Windows下的Eclipse 默认会去读取系统的编码,所以Widnwos下的Eclipse的编码 ...

  2. JavaScript学习笔记——对象基础

    javascript对象基础 一.名词解释: 1.基于对象 一切皆对象,以对象的概念来编程. 2.面向对象编程(oop Object oriented programming) A.对象 就是人们要研 ...

  3. 给IOS系统的微信页面赋Title

    给页面赋一个title是最平常不过的事情了,不过在IOS下动态给页面赋title可不是平常的事情. 看代码: function setIOStitle(title) { $body = $('body ...

  4. 其他系统与ecshop的会员整合

    步骤一:整合两个的会员数据 用软件Navicat 的 "导入向导"功能,导入你的原数据类型(sql,mdb,db)我的是mdb类型.下一步选择你原有的会员字段“user”.再进行下 ...

  5. jquery左边滚动,完毕后跳转回来

    代码如下 function in_scroll(){ $liW = ; $num = $(".in-cy li").size(); $ulW = $liW*$num; $(&quo ...

  6. [Unity] 常用技巧收集

    Unity 屏幕旋转 void Update () { //处理横向两个方向旋转 if(Input.deviceOrientation == DeviceOrientation.LandscapeLe ...

  7. jQuery 元素的选中, 置顶、上移、下移、置底、删除

    如截图: <ul> <li class="li01" onclick="C_columnSetTop(this)"><i>& ...

  8. Source Insight 基本使用(2)-修改Source Insight 快捷键

    1. 首先,打开source insight主界面. 2. 选择"options->key assignments",进入快捷键设置界面. 3. 此时,可以看到快捷键设置对话 ...

  9. css1-css3的那些模糊点

    css很重要, 但也不是万能的, 也不能抛弃dom 元素和 元素的属性!! 很多时候, dom "元素" 的 "属性" 也很重要 也很实用! 要结合属性来写 包 ...

  10. IEnumerable 和 IEnumerator

    IEnumerable 接口只包含一个抽象的方法 GetEnumerator(),它返回一个可用于循环访问集合的 IEnumerator 对象,IEnumerator 对象是一个集合访问器. 需要给自 ...