A.NSOperation的基本使用
1.NSOperation的作用
配合使用NSOperation和NSOperationQueue也能实现多线程编程

NSOperation和NSOperationQueue实现多线程的具体步骤
先将需要执行的操作封装到一个NSOperation对象中
然后将NSOperation对象添加到NSOperationQueue中
系统会自动将NSOperationQueue中的NSOperation取出来
将取出的NSOperation封装的操作放到一条新线程中执行

 
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使用它的子类

使用NSOperation子类的方式有3种
NSInvocationOperation
NSBlockOperation
自定义子类继承NSOperation,实现内部相应的方法

 
3.NSInvocationOperation
创建NSInvocationOperation对象
- (id)initWithTarget:(id)target selector:(SEL)sel object:(id)arg;

调用start方法开始执行操作
- (void)start;
一旦执行操作,就会调用target的sel方法

注意
默认情况下,调用了start方法后并不会开一条新线程去执行操作,而是在当前线程同步执行操作
只有将NSOperation放到一个NSOperationQueue中,才会异步执行操作

 
4.NSBlockOperation
创建NSBlockOperation对象
+ (id)blockOperationWithBlock:(void (^)(void))block;

通过addExecutionBlock:方法添加更多的操作
- (void)addExecutionBlock:(void (^)(void))block;

注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作

 
5.NSOperationQueue
NSOperationQueue的作用
NSOperation可以调用start方法来执行任务,但默认是同步执行的
如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作

添加操作到NSOperationQueue中
- (void)addOperation:(NSOperation *)op;
- (void)addOperationWithBlock:(void (^)(void))block;

 
6.最大并发数
什么是并发数
同时执行的任务数
比如,同时开3个线程执行3个任务,并发数就是3

最大并发数的相关方法
- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;

 
7.队列的取消、暂停和恢复
取消队列的所有操作
- (void)cancelAllOperations;
提示:也可以调用NSOperation的- (void)cancel方法取消单个操作

暂停和恢复队列
- (void)setSuspended:(BOOL)b; // YES代表暂停队列,NO代表恢复队列
- (BOOL)isSuspended;

 
8.操作优先级
设置NSOperation在queue中的优先级,可以改变操作的执行优先级
- (NSOperationQueuePriority)queuePriority;
- (void)setQueuePriority:(NSOperationQueuePriority)p;

优先级的取值
NSOperationQueuePriorityVeryLow = -8L,
NSOperationQueuePriorityLow = -4L,
NSOperationQueuePriorityNormal = 0,
NSOperationQueuePriorityHigh = 4,
NSOperationQueuePriorityVeryHigh = 8

 
9.操作依赖
NSOperation之间可以设置依赖来保证执行顺序
比如一定要让操作A执行完后,才能执行操作B,可以这么写
[operationB addDependency:operationA]; // 操作B依赖于操作A

可以在不同queue的NSOperation之间创建依赖关系

 
10.操作监听
可以监听一个操作的执行完毕
- (void (^)(void))completionBlock;
- (void)setCompletionBlock:(void (^)(void))block;
 
11.自定义NSOperation
自定义NSOperation的步骤很简单
重写- (void)main方法,在里面实现想执行的任务

重写- (void)main方法的注意点
自己创建自动释放池(因为如果是异步操作,无法访问主线程的自动释放池)
经常通过- (BOOL)isCancelled方法检测操作是否被取消,对取消做出响应

 
B.使用自定义NSOperation后台下载图片
 
 
code source: https://github.com/hellovoidworld/ConcurrentDownloadImageDemo
 
 
1.思路
 
 
 
2.实现步骤
(1)自定义一个继承NSOperation的类,实现main方法,在main方法中编写任务事件
 //
// HVWDownloadImageOperation.h
// ConcurrentDownloadImageDemo
//
// Created by hellovoidworld on 15/1/22.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @class HVWDownloadImageOperation; @protocol HVWDownloadImageOperationDelegate <NSObject> /** 代理方法,下载完成之后 */
@optional
- (void) downloadImageOperation:(HVWDownloadImageOperation *) operation didFinishedDownloadWithImage:(UIImage *) image; @end @interface HVWDownloadImageOperation : NSOperation /** 存储每个图片的url */
@property(nonatomic, strong) NSString *url; /** 要显示的cell的index */
@property(nonatomic, strong) NSIndexPath *indexPath; /** 代理 */
@property(nonatomic, weak) id<HVWDownloadImageOperationDelegate> delegate; @end
 
 //
// HVWDownloadImageOperation.m
// ConcurrentDownloadImageDemo
//
// Created by hellovoidworld on 15/1/22.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h>
#import "HVWDownloadImageOperation.h" @implementation HVWDownloadImageOperation - (void)main {
NSLog(@"====下载图片======%@", [NSThread currentThread]); NSURL *url = [NSURL URLWithString:self.url];
NSData *data;
for (int i=; i<; i++) {
data = [NSData dataWithContentsOfURL:url];
} UIImage *image = [UIImage imageWithData:data]; /** 调用代理方法,通知代理下载完成 */
if ([self.delegate respondsToSelector:@selector(downloadImageOperation:didFinishedDownloadWithImage:)]) {
[self.delegate downloadImageOperation:self didFinishedDownloadWithImage:image];
}
} @end
 
(2)新建一个模型类,用来存储每个cell的数据
 //
// HVWApp.h
// ConcurrentDownloadImageDemo
//
// Created by hellovoidworld on 15/1/22.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWApp : NSObject /** app名字 */
@property(nonatomic, strong) NSString *name;
/** app图标url */
@property(nonatomic, strong) NSString *icon;
/** app的副标题--下载量 */
@property(nonatomic, strong) NSString *download; - (instancetype) initWithDictionary:(NSDictionary *) dict;
+ (instancetype) appWithDictionary:(NSDictionary *) dict; @end
 
(3)在控制器中编写调用任务、队列进行多线程并发后台下载图片的操作
 //
// ViewController.m
// ConcurrentDownloadImageDemo
//
// Created by hellovoidworld on 15/1/22.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "HVWApp.h"
#import "HVWDownloadImageOperation.h" @interface ViewController () <UITableViewDataSource, UITableViewDelegate, HVWDownloadImageOperationDelegate> /** 所有app数据 */
@property(nonatomic, strong) NSArray *apps; /** 任务队列 */
@property(nonatomic, strong) NSOperationQueue *queue; /** 所有任务 */
@property(nonatomic, strong) NSMutableDictionary *operations; /** 所有图片 */
@property(nonatomic, strong) NSMutableDictionary *images; @end @implementation ViewController /** 加载plist文件,读取数据到模型,存储到数组中 */
- (NSArray *)apps {
if (nil == _apps) {
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]]; NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
HVWApp *app = [HVWApp appWithDictionary:dict];
[appArray addObject:app];
}
_apps = appArray;
}
return _apps;
} - (NSOperationQueue *)queue {
if (_queue == nil ) {
_queue = [[NSOperationQueue alloc] init];
_queue.maxConcurrentOperationCount = ;
}
return _queue;
} - (NSMutableDictionary *)operations {
if (nil == _operations) {
_operations = [NSMutableDictionary dictionary];
}
return _operations;
} - (NSMutableDictionary *)images {
if (nil == _images) {
_images = [NSMutableDictionary dictionary];
}
return _images;
} - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - tableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"AppCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
} HVWApp *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download; // 占位图片
cell.imageView.image = [UIImage imageNamed:@"a9ec8a13632762d0092abc3ca2ec08fa513dc619"]; // 如果没有图片,准备开启线程下载图片
UIImage *image = self.images[app.icon]; if (image) {
// 如果图片存在,不需要重复下载,直接设置图片
cell.imageView.image = image;
} else { // 如果图片不存在,看看是不是正在下载
HVWDownloadImageOperation *operation = self.operations[app.icon]; if (operation) {
// 如果图片正在下载,不必要开启线的线程再进行下载
} else { // 没有在下载,创建一个新的任务进行下载
operation = [[HVWDownloadImageOperation alloc] init];
// 设置代理
operation.delegate = self;
// 传送url
operation.url = app.icon;
// 记录indexPath
operation.indexPath = indexPath; [self.queue addOperation:operation]; // 记录正在下载的任务
[self.operations setObject:operation forKey:operation.url];
}
} return cell;
} #pragma mark - HVWDownloadImageOperationDelegate
/** 代理方法,图片下载完成后,显示到cell上 */
- (void)downloadImageOperation:(HVWDownloadImageOperation *)operation didFinishedDownloadWithImage:(UIImage *)image {
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:operation.indexPath];
cell.imageView.image = image;
[self.tableView reloadRowsAtIndexPaths:@[operation.indexPath] withRowAnimation:UITableViewRowAnimationNone]; // 存储图片到内存
if (image) {
[self.images setObject:image forKey:operation.url];
} NSLog(@"已经下载的图片数==========>%d", self.images.count);
} @end
 
 

[iOS 多线程 & 网络 - 1.3] - NSOperation的更多相关文章

  1. [iOS 多线程 & 网络 - 1.0] - 多线程概述

    A.进程 什么是进程进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开QQ.Xcode,系统就会分别启动2个进程 通过"活 ...

  2. iOS 多线程 简单学习NSThread NSOperation GCD

    1:首先简单介绍什么叫线程 可并发执行的,拥有最小系统资源,共享进程资源的基本调度单位. 共用堆,自有栈(官方资料说明iOS主线程栈大小为1M,其它线程为512K). 并发执行进度不可控,对非原子操作 ...

  3. [iOS 多线程 & 网络 - 2.0] - 发送接收 服务器信息

    A.搭建java服务器 使用eclipse.tomcat和struts2框架搭建一个简单的服务器 1.准备好合适版本的JDK.eclipse EE.tomcat.struts2 框架包 2.配置JDK ...

  4. [iOS 多线程 & 网络 - 2.9] - ASI框架

    A.ASI基本知识 1.ASI简单介绍 ASI:全称是ASIHTTPRequest,外号“HTTP终结者”,功能十分强大. ASI的实现基于底层的CFNetwork框架,因此运行效率很高. ASI的g ...

  5. [iOS 多线程 & 网络 - 2.8] - 检测网络状态

    A.说明 在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验 ...

  6. [iOS 多线程 & 网络 - 2.3] - 解析xml

    A.XML基本知识 1.xml概念 什么是XML全称是Extensible Markup Language,译作“可扩展标记语言”跟JSON一样,也是常用的一种用于交互的数据格式一般也叫XML文档(X ...

  7. [iOS 多线程 & 网络 - 2.1] - 解析json

    A.iOS中json的基本使用 1.解析json数据 (1)json反序列化 对象{}格式 {key : value, key : value,...} 的键值对的结构可以反序列化为OC中的NSDic ...

  8. [iOS 多线程 & 网络 - 1.1] - 多线程NSThread

    A.NSThread的基本使用 1.创建和启动线程 一个NSThread对象就代表一条线程创建.启动线程NSThread *thread = [[NSThread alloc] initWithTar ...

  9. [iOS 多线程 & 网络 - 4.0] - AFN框架简单使用

    A.AFN基本知识 1.概念 AFNetworking 是对NSURLConnection的封装 运行效率没有ASI高(因为ASI基于CFNetwork),但是使用简单 AFN支持ARC     B. ...

随机推荐

  1. 转载:C++ STL set学习

    声明:本文转载自Penguin的博客 http://blog.sina.com.cn/s/blog_779cf3410101389s.html 1,set的含义是集合,它是一个有序的容器,里面的元素都 ...

  2. Alpha、Beta、RC、GA版本的区别

    Alpha:是内部测试版,一般不向外部发布,会有很多Bug.一般只有测试人员使用. Beta:也是测试版,这个阶段的版本会一直加入新的功能.在Alpha版之后推出. RC:(Release Candi ...

  3. textContent、innerText的用法,在文档中插入纯文本

    有时候需要查询纯文本形式的元素内容,或者在文档中插入纯文本.标准的方法是用Node的textContent属性来实现: var para = document.getElementsByTagName ...

  4. acdream 1210 Chinese Girls' Amusement (打表找规律)

    题意:有n个女孩围成一个圈从第1号女孩开始有一个球,可以往编号大的抛去(像传绣球一样绕着环来传),每次必须抛给左边第k个人,比如1号会抛给1+k号女孩.给出女孩的人数,如果他们都每个人都想要碰到球一次 ...

  5. ffmpeg 从内存中读取数据(或将数据输出到内存)

    更新记录(2014.7.24): 1.为了使本文更通俗易懂,更新了部分内容,将例子改为从内存中打开. 2.增加了将数据输出到内存的方法. 从内存中读取数据 ffmpeg一般情况下支持打开一个本地文件, ...

  6. IPicture、BITMAP、HBITMAP和CBitmap的关系

    1.有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP转换IPicture)Picture的方法get_Handle可以直接得到图片的句柄 IPicture *pIPict ...

  7. Xcode中如何启用或禁用某些文件的ARC

    经常会有工程中涉及到第三方的代码, 但这些代码有的是ARC的, 有的不是. 这样的话, 在与你的工程中集成的时候就会出现问题. 如果你的工程是开启ARC的, 那就需要对某些文件禁用ARC, (-fno ...

  8. Android Message和obtainMessage的区别

    类概述 定义一个包含任意类型的描述数据对象,此对象可以发送给Handler.对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作. 尽管Message的构造器是公开 ...

  9. mysql explain中key_len的计算

    ken_len表示索引使用的字节数,根据这个值,就可以判断索引使用情况,特别是在组合索引的时候,判断是否所有的索引字段都被查询用到. key_len显示了条件检索子句需要的索引长度,但 ORDER B ...

  10. hashCode之一--两个对象值相同,有相同的hash code

    两个对象值相同(x.equals(y) == true),则一定有相同的hash code. 这是java语言的定义:  因为:Hash,一般翻译做“散列”,也有直接音译为"哈希" ...