以前看过,ios上主要有3种多线程开发的方式:
1、NSThread以及基于它的performSelector方法
2、NSOperation和NSOperationQueue
3、GCD
我们的项目最早使用的是NSThread,后来全部换成了GCD,但是NSOperation就从来没试过,今天也稍微看了下
感觉还比较简单,一共只有2个类,分别是NSOperation和NSOperationQueue
NSOperation类似于java中的Runnable接口,需要实例化才能用,不过cocoa已经提供了2个现成的子类,NSInvocationOperation和NSBlockOperation,一般情况下,用这2种现成的就可以了,特殊情况才需要自定义。创建好NSOperation后,将其放到NSOperationQueue里就会运行了,示例代码:
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testMethod) object:nil];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];
指定的方法可以有一个参数,也可以有返回值,比performSelect稍微强点。另外利用addDependency方法,可以很方便地设置任务的依赖顺序:
NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testMethod:) object:nil];
NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(testMethod2) object:nil];
[operation2 addDependency:operation1];// 这行代码保证了执行的先后顺序
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation2];// 即使operation2先放入队列,也会在operation1之后执行
[queue addOperation:operation1];
感觉NSOperation的抽象层次比GCD更高,所以API调用更加简单。不过暂时并不打算修改,先你了解一下,或许下次会使用它
关于GCD和NSOperation的比较,下面几篇帖子都不错:
GCD和NSOperation的比较
使用NSOperation简化多线程开发
分享到:
相关推荐
NSOperation NSOperationQueue demo 项目讲述了 NSOperation 和 NSOperationQueue 怎么使用。适合学习
该demo详细解释了gcd NSThread NSOperationQueue的用法.详细解释了子线程中如何刷新UI
NSOperationQueue + CompletionBlock NSOperationQueue的完成块。 乍看上去 Swift import NSOperationQueue_CompletionBlock let queue = NSOperationQueue () queue. completionBlock = { NSLog ( " I'm done! ...
TRVSURLSessionOperation 通过 。 NSURLSession和NSOperationQueue一起工作 TRVSURLSessionOperation是一个NSOperation子类,该子类包装了NSURLSessionTask因此...NSOperationQueue *queue = [[ NSOperationQueue all
利用 NSOperationQueue 实现摇奖机功能。 掌握 NSOperationQueue 的暂停功能结合业务实现效果
使用 NSOperationQueue 优化网络的 iOS 项目 概述 该项目旨在提供一种优化网络的简单方法,重点是使用 NSOperationQueue,而不是简单地使用 NSURLConnection 的异步 API 或使用 GCD 将工作卸载到辅助队列,后者对...
方便的NSOperationQueue和NSBlockOperation帮助器。 SSOperations在我的应用程序各种操作提供了。 安装 与安装。 添加到您的Podfile : pod 'SSOperations', :head # YOLO SSBlockOperation和...
一、NSOperation简介 1.简单说明 NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤: (1)先将需要执行的操作封装到一个...
配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加到NSOperationQueue中 ...
iOS并发编程指南, 深入讲解iOS在编发编程中常用的GCD、NSOperation、NSOperationQueue的使用原理
简介: 该框架是一个支持单任务下载和批量下载的下载框架,实现方式比较抽象,如果大家有好的思路,可以分享交流一下,生命不止,学习不息。轰 轰 轰~ 1.该下载框架采用自定义NSOperation来封装下载任务,并手动...
介绍多种开辟子线程的方法,包括:NSThread,NSOperationQueue,GCD等
第一部分为抖音个人主页实现,包含NSOperationQueue多队列解析webp动图、网络资源拓扑云框架搭建。第二部分围绕AVPlayerLayer展开,涉及网络视频边播放边下载、 UITableView控制多个视频源播放。第三部分则为...
对iOS的多线程方式做的总结;Pthreads,NSThread,GCD,NSOperation/NSOperationQueue
GCD和NSOperationQueue的⽐比较,各⾃自优缺点是什么,平时你是怎么使⽤用的? weak,assign,strong的意义和区别? atomic,nonatomic的意义和区别? OC中有没有多继承?OC中怎么实现多继承功能? category和subclass的...
1. 如何通过 NSURLConnection + NSOperation + NSOperationQueue 网络下载Json 文件。 2. 如何把Json 文件转换成自定义的数据结构。 3. Core Animation: 常用的 2D 和 3D 动画技巧和实现。 4. 如何支持多语言 (这个...
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
苹果官方文档的翻译,详细讲解。NSOperation+NSOperationQueue,GCD+dispatch_queue,RunLoop等详细介绍。
#iOS的三种多线程技术 和特性 1.NSThread 2.NSOperation/NSOperationQueue 3.GCD (大中心调度)
XHNewsParsing是使用Swift语言编写的新浪新闻客户端。 1、使用NSOperationQueue和NSURLConnection下载网络数据。 2、使用UITableView显示消息数据。