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开启一个线程来执行.如果是并 ...
随机推荐
- Java排序算法——插入排序
import java.util.Arrays; //================================================= // File Name : Select_S ...
- Android学习笔记——ListView
该工程的功能是实现在一个activity中显示一个列表 以下代码是MainActivity.java中的代码 package com.example.listview; import java.uti ...
- Java 生成验证码图片
生成验证码图片并对提交的输入进行验证 // HttpServletResponse常见应用——生成验证码 // 利用BufferedImage类生产随机图片 public static final i ...
- nginx重定向配置
# /etc/nginx/nginx.conf #写在server,location核心模块中,if也可以写.$http_host客户端设法要到达主机的主机名 if ($http_host !~ “^ ...
- 2012 Theory for Forward Rendering
http://miss-cache.blogspot.com/2012/08/lighting-transparent-surfaces-with_26.html http://aras-p.info ...
- Windows系统bug
今天,发现Windows系统的一个bug 也不知道是不是bug,未深入 在网上下载图片,将图像另存为到本地的时候,图片文件名可以为空(后缀要保留) 但是,在本地,是无法直接将文件名命名为空的~
- 浅谈T-SQL中的子查询
引言 这篇文章我们来简单的谈一下子查询的相关知识.子查询可以分为独立子查询和相关子查询.独立子查询不依赖于它所属的外部查询,而相关子查询则依赖于它所属的外部查询.子查询返回的值可以是标量(单值).多值 ...
- fopen()和fclose()的用法
fopen()和fclose()的用法 1.fopen()函数的用法 fopen函数用于打开文件, 其调用格式为: FILE *fopen(char *filename, *type); fopen( ...
- ubuntu系统下更新jdk版本
1. 添加软件源 sudo add-apt-repository ppa:webupd8team/java 2. 更新软件源 sudo apt-get update 3. 安装 jdk1.8 sudo ...
- php去掉字符串的最后一个字符附substr()的用法
转自:http://www.jb51.net/article/26604.htm 今天项目中用到,去掉字符串中的最后一个字符 原字符串1,2,3,4,5,6, 去掉最后一个字符"," ...