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开启一个线程来执行.如果是并 ...
随机推荐
- Android学习笔记——ListView
该工程的功能是实现在一个activity中显示一个列表 以下代码是MainActivity.java中的代码 package com.example.listview; import java.uti ...
- CentOS系统rsync文件同步 安装配置
rsync是类unix系统下的数据镜像备份工具,从软件的命名上就可以看出来了——remote sync 它的特性如下: 可以镜像保存整个目录树和文件系统. 可以很容易做到保持原来文件的权限.时间.软硬 ...
- HDFS的Java操作
实验环境: Windows 10 Eclipse Mars.2 Release (4.5.2) CentOS 7 Hadoop-2.7.3 先决条件: 1) Windows上各环境变量已配置OK. ...
- sublime2的快捷键
1.快速文件转换 与sublime2中搜索文件快捷键一样,ctrl+p ctrl + p //window comm + p //mac 2.在代码中搜索 在所有已加载的文件中查找一个特定的字符串,支 ...
- jquery 停止animate动画,并且回复最初状态
// 热门推荐悬浮效果 $("#recom_con li img").mouseenter(function(){ $(this).stop(true, true); $w = p ...
- js确定要删除吗
js代码 function confirm_redirect(msg, url) { if (confirm(msg)) { location.href=url; } } html <a hre ...
- iOS后台播放
### 音乐后台播放 * .当程序进入后台的时候,开启后台任务 ``` - (void)applicationDidEnterBackground:(UIApplication *) { // 开启后 ...
- 创建为ClickOnce清单签名的.pfx格式数字证书
------ 第一步 创建 X.509 证书 ------makecert.exe为证书创建工具.证书创建工具生成仅用于测试目的的 X.509 证书.它创建用于数字签名的公钥和私钥对,并将其存储在证书 ...
- 固定IP 正常访问谷歌
如题 地址栏直接输入 http://173.194.1.150/ 正常使用 ~标记一下~
- 测试LVS+Keepalived高可用负载均衡集群
测试LVS+Keepalived高可用负载均衡集群 1. 启动LVS高可用集群服务 此时查看Keepalived服务的系统日志信息如下: [root@localhost ~]# tail -f /va ...