iOS多线程与网络开发之NSOperation

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。

如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^

我要捐赠: 点击捐赠

Cocos2d-X源码下载:点我传送

游戏官方下载:http://dwz.cn/RwTjl

游戏视频预览:http://dwz.cn/RzHHd

游戏开发博客:http://dwz.cn/RzJzI

游戏源码传送http://dwz.cn/Nret1

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后台下载图片

实现效果如下:

1.思路

2.实现步骤[dian wo xia zai资源分不足下方留邮箱]

(1)自定义一个继承NSOperation的类,实现main方法,在main方法中编写任务事件

//
//  HVWDownloadImageOperation.h
//  ConcurrentDownloadImageDemo
//
//  Created by YuriyHao on 15/7/24.
//  Copyright (c) 2015年 YuriyHao. All rights reserved.
//

#import <Foundation/Foundation.h>

@class HVWDownloadImageOperation;

@protocol HVWDownloadImageOperationDelegate <NSObject>

@optional
- (void) downloadImageOperation:(HVWDownloadImageOperation *) operation didFinishedDownloadWithImage:(UIImage *) image;

@end

@interface HVWDownloadImageOperation : NSOperation

@property(nonatomic, strong) NSString *url;

@property(nonatomic, strong) NSIndexPath *indexPath;

@property(nonatomic, weak) id<HVWDownloadImageOperationDelegate> delegate;

@end

//
//  HVWDownloadImageOperation.m
//  ConcurrentDownloadImageDemo
//
//  Created by YuriyHao on 15/7/24.
//  Copyright (c) 2015年 YuriyHao. 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=0; i<1; 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 YuriyHao on 15/7/24.
//  Copyright (c) 2015年 YuriyHao. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HVWApp : NSObject

@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *icon;
@property(nonatomic, strong) NSString *download;

- (instancetype) initWithDictionary:(NSDictionary *) dict;
+ (instancetype) appWithDictionary:(NSDictionary *) dict;

@end

(3)在控制器中编写调用任务、队列进行多线程并发后台下载图片的操作

//
//  ViewController.m
//  ConcurrentDownloadImageDemo
//
//  Created by YuriyHao on 15/7/24.
//  Copyright (c) 2015年 YuriyHao. All rights reserved.
//

#import "ViewController.h"
#import "HVWApp.h"
#import "HVWDownloadImageOperation.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, HVWDownloadImageOperationDelegate>

@property(nonatomic, strong) NSArray *apps;

@property(nonatomic, strong) NSOperationQueue *queue;

@property(nonatomic, strong) NSMutableDictionary *operations;
@property(nonatomic, strong) NSMutableDictionary *images;

@end

@implementation ViewController

- (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 = 3;
    }
    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 1;
}

- (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
- (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

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。

如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^

我要捐赠: 点击捐赠

Cocos2d-X源码下载:点我传送

游戏官方下载:http://dwz.cn/RwTjl

游戏视频预览:http://dwz.cn/RzHHd

游戏开发博客:http://dwz.cn/RzJzI

游戏源码传送http://dwz.cn/Nret1

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 10:46:48

iOS多线程与网络开发之NSOperation的相关文章

iOS多线程与网络开发之NSURLCache

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 A.基本知识 1.为什么需要缓存? 有时候一个url会请求多次,得到的内容确实一样的 2.缓存的

Android网络开发之用tcpdump抓包

Android开发过程中,当涉及到网络通信的时候,有一些字段需要抓包获取.我之前因为SSDP设备发现的包头格式没有写对,经过抓包分析和标准包头对比发现了这个困扰我很久的问题.总之,掌握在Android手机里面抓包是很有必要的. 准备工作:Android系统的手机,网络环境,tcpdump,破解手机root权限,建议最好在手机里面安装RE文件管理器并且给root权限.具体步骤如下: 首先,通过adb工具将tcpdump推送到手机,tcpdump的下载地址为:http://www.strazzere

Qt网络开发之QNetworkAccessManager 及 qt4使用QUdpSocket发送数据报datagrams

一:Qt网络开发之QNetworkAccessManager  (http://m.blog.csdn.net/blog/u010002704/41355917) 一个应用程序,一个QNetworkAccessManager就足够了 每一个回复QNetworkReply都需要删除,否则会出现内存泄露,根据Qt的帮助文档,在接收完数据的槽中使用deleteLater(),防止内存泄露. 二:Qt4使用QUdpSocket发送数据报datagrams UDP服户端绑定广播[喝小酒的网摘]http:/

iOS多线程开发之NSOperation - 快上车,没时间解释了!

一.什么是NSOperation? NSOperation是苹果提供的一套多线程解决方案.实际上NSOperation是基于GCD更高一层的封装,但是比GCD更加的面向对象.代码可读性更高.可控性更强,很屌的是加入了操作依赖. 默认情况下,NSOperation单独使用时只能同步执行操作,并没有开辟新线程的能力,只有配合NSOperationQueue才能实现异步执行.讲到这里,我们不难发现GCD和NSOperation实现的方式很像,其实这更像是废话,NSOperation本身就是基于GCD的

iOS多线程与网络开发之多线程概述

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源码下载:点我传送 游戏官方下载:http://dwz.cn/RwTjl 游戏视频预览:http://dwz.cn/RzHHd 游戏开发博客:http://dwz.cn/RzJzI 游戏源码传送:http://dwz.cn/Nret1 A.进程 什么是进程进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行

iOS开发笔记--敏捷开发之Scrum扫盲篇

敏捷开发之Scrum扫盲篇 现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中的各个环节,主要 目的有两个,一个是进行知识的总结,另外一个是觉得网上很多学习资料的讲述方式让初学者不太容易理解:所以我决定写一篇扫盲性的博文,同时试着也与园内的 朋友一起分享交流一下,希望对初学者有帮助.  什么是敏捷开发? 敏捷开发(Agile Development

ios多线程操作(十一)—— NSOperation的高级操作

NSOperation可以调用start方法来执行任务,但默认是同步执行的,如果将NSOperation添加到      NSOperationQueue(操作队列)中,系统会自动异步执行队列中的操作. NSOperationQueue中可以对操作进行取消,暂停和恢复 取消函数为: [objc] view plain copy - (void)cancelAllOperations; 也可以调用NSOperation的cancel方法来取消单个操作 [objc] view plain copy

iOS开发之NSOperation &amp; NSOperationQueue

1.简介 (1) NSOperationQueue(操作队列)是由GCD提供的队列模型的Cocoa抽象,是一套Objective-C的API,为了使并发(多线程)编程变得更加简单,但效率比GCD略低.在实际开发中NSOperationQueue是首选. (2) GCD提供了更加底层的控制,而操作队列则在GCD之上实现了一些方便的功能,这些功能对于开发者而言通常是最好最安全的选择. 队列及操作 (1)NSOperationQueue有两种不同类型的队列:主队列和自定义队列 (2)主队列运行在主线程

iOS网络开发之AFNetworking

概述 AFNetworking是一个非常受欢迎的轻量级的iOS.Mac OS X网络通信类库.它建立在NSURLConnection.NSOperation以及其技术的基础上,有着精心设计的模块结构和功能丰富的API,让很多网络通信功能的实现变得十分简单. AFNetworking支持HTTP请求和基于REST的网络服务(包括GET.POST. PUT以及DELETE等).支持ARC.AFNetworking项目中还包含一些列单元测试. 要求iOS 5.0及以上版本,或者Mac OS 10.7及