继承NSOperation类完成能被cancel的耗时操作
来源:互联网 发布:office电脑版办公软件 编辑:程序博客网 时间:2024/06/13 04:00
//
// 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 = [[superalloc] 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 {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MyOperation *op1 = [MyOperationoperationWithOpname:@"op1"];
MyOperation *op2 = [MyOperationoperationWithOpname:@"op2"];
[op1 setCompletionBlock:^{
NSLog(@"op1 finish");
}];
[op2 setCompletionBlock:^{
NSLog(@"op2 finish");
}];
self.queue = [[NSOperationQueuealloc] init];
[self.queueaddOperation:op1];
[self.queueaddOperation: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
- 继承NSOperation类完成能被cancel的耗时操作
- 使用SwingWorker类完成耗时操作
- NSOperation的继承实现
- 代码的耗时操作
- service是否在main thread中执行, service里面是否能执行耗时的操作?
- [RunLoop 让返回值在耗时操作完成后返回]
- 广播在服务中接收能耗时操作吗
- NSOperation基本的一些操作
- ios-NSOperation的操作依赖
- 使用c++完成一个不被继承的类
- Android面试题-Service是否在main thread中执行, service里面是否能执行耗时的操作?
- Android中Service中是否能做耗时操作,两种启动模式与服务不被杀死的方法
- 耗时两周的P2PQQ终于完成了!!!
- 关于C# 耗时操作的时间查看
- iOS对耗时操作的处理方法
- MFC 处理耗时操作的一种办法
- 不同内存区域的耗时操作
- 封装HttpUtil访问网络的耗时操作
- Xcode 工程文件打开不出来, cannot be opened because the project file cannot be parsed.
- 从僵尸网络追踪到入侵检测 第2章 检测环境
- 行政划分
- 《现代控制工程》负载元件方程的推导
- 安装nginx踩到的坑
- 继承NSOperation类完成能被cancel的耗时操作
- android sdk 更新
- Markdown学习 - 基本使用
- Java调用Python时的编码问题
- MyBatis_动态SQL
- 共享免费socks5账号
- 带动画的自定义view——做一个移动的箭头
- 【设计模式】工厂三模式(创建型)
- 个人技能准备