继承NSOperation类完成能被cancel的耗时操作
2016-01-09 15:02
417 查看
//
// MyOperation.h
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MyOperation :
NSOperation
+ (instancetype)operationWithOpname:(NSString *)opname;
@end
//
// MyOperation.m
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "MyOperation.h"
@interface
MyOperation ()
@property (nonatomic,
copy) NSString *opname;
@end
@implementation MyOperation
+ (instancetype)operationWithOpname:(NSString *)opname {
MyOperation *operation = [[super
alloc] init];
operation.opname = opname;
return operation;
}
- (void)main {
for (int i =
0; i < 1000; i++) {
if (self.isCancelled) {
return;
}
NSLog(@"%@--%d",
self.opname, i);
}
}
@end
//
// ViewController.m
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ViewController.h"
#import "MyOperation.h"
@interface
ViewController ()
@property (nonatomic,
strong) NSOperationQueue *queue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyOperation *op1 = [MyOperation
operationWithOpname:@"op1"];
MyOperation *op2 = [MyOperation
operationWithOpname:@"op2"];
[op1 setCompletionBlock:^{
NSLog(@"op1 finish");
}];
[op2 setCompletionBlock:^{
NSLog(@"op2 finish");
}];
self.queue = [[NSOperationQueue
alloc]
init];
[self.queue
addOperation:op1];
[self.queue
addOperation:op2];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1
* NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[op2 cancel];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1
* NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[op1 cancel];
});
}
@end
// MyOperation.h
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface MyOperation :
NSOperation
+ (instancetype)operationWithOpname:(NSString *)opname;
@end
//
// MyOperation.m
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "MyOperation.h"
@interface
MyOperation ()
@property (nonatomic,
copy) NSString *opname;
@end
@implementation MyOperation
+ (instancetype)operationWithOpname:(NSString *)opname {
MyOperation *operation = [[super
alloc] init];
operation.opname = opname;
return operation;
}
- (void)main {
for (int i =
0; i < 1000; i++) {
if (self.isCancelled) {
return;
}
NSLog(@"%@--%d",
self.opname, i);
}
}
@end
//
// ViewController.m
// NSOperation
//
// Created by zmx on 16/1/8.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ViewController.h"
#import "MyOperation.h"
@interface
ViewController ()
@property (nonatomic,
strong) NSOperationQueue *queue;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyOperation *op1 = [MyOperation
operationWithOpname:@"op1"];
MyOperation *op2 = [MyOperation
operationWithOpname:@"op2"];
[op1 setCompletionBlock:^{
NSLog(@"op1 finish");
}];
[op2 setCompletionBlock:^{
NSLog(@"op2 finish");
}];
self.queue = [[NSOperationQueue
alloc]
init];
[self.queue
addOperation:op1];
[self.queue
addOperation:op2];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1
* NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[op2 cancel];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1
* NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[op1 cancel];
});
}
@end
相关文章推荐
- Python3写爬虫(四)多线程实现数据爬取
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#实现多线程的同步方法实例分析
- 浅谈chuck-lua中的多线程
- C#简单多线程同步和优先权用法实例
- C#多线程学习之(四)使用线程池进行多线程的自动管理
- C#多线程编程中的锁系统(三)
- C#多线程学习之(六)互斥对象用法实例
- 基于一个应用程序多线程误用的分析详解
- C#多线程学习之(三)生产者和消费者用法分析
- C#多线程学习之(一)多线程的相关概念分析
- C#多线程之Thread中Thread.IsAlive属性用法分析
- 分享我在工作中遇到的多线程下导致RCW无法释放的问题
- C#多线程编程之使用ReaderWriterLock类实现多用户读与单用户写同步的方法