iOS开发多线程篇 - NSOperation(上)
2023-09-27 14:25:57 时间
一. NSOperation简介1. 简单说明
[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。
NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤
创建对象和执行操作
NSInvocationOperation * operation [[NSInvocationOperation alloc]initWithTarget:self selector: selector(testTarget) object:nil]; [operation start];
说明 一旦执行操作 就会调用Target的testTarget方法
代码示例
// // ViewController.m // TestNSOperationQueue // Created by taobaichi on 2017/3/21. // Copyright © 2017年 MaChao. All rights reserved. #import ViewController.h interface ViewController () implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSInvocationOperation * operation [[NSInvocationOperation alloc]initWithTarget:self selector: selector(testTarget) object:nil]; [operation start]; -(void)testTarget { NSLog( -------test---% --- ,[NSThread currentThread]); }
打印结果
2017-03-21 11:16:05.385 TestNSOperationQueue[3648:99757] -------test--- NSThread: 0x6080000775c0 {number 1, name main}---
注意 操作对象默认在主线程中执行 只有添加到队列中才会开启新的线程 即默认情况下 如果操作没有放到队列中queue中 都是同步执行 只有将NSOperation放到一个NSOperationQueue中 才会以异步执行
NSBlockOperation * operation [NSBlockOperation blockOperationWithBlock:^{ //...... operation addExecutionBlock:^{ //...... };
// // ViewController.m // TestNSOperationQueue // Created by taobaichi on 2017/3/21. // Copyright © 2017年 MaChao. All rights reserved. #import ViewController.h interface ViewController () implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSBlockOperation * operation [NSBlockOperation blockOperationWithBlock:^{ NSLog( NSBlockOperation------% ,[NSThread currentThread]); [operation start];
end
打印结果
2017-03-21 11:37:21.540 TestNSOperationQueue[4033:113489] NSBlockOperation------ NSThread: 0x60800006a3c0 {number 1, name main}
// // ViewController.m // TestNSOperationQueue // Created by taobaichi on 2017/3/21. // Copyright © 2017年 MaChao. All rights reserved. #import ViewController.h interface ViewController () implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSBlockOperation * operation [NSBlockOperation blockOperationWithBlock:^{ NSLog( NSBlockOperation------% ,[NSThread currentThread]); [operation addExecutionBlock:^{ NSLog( NSBlockOperation1------% ,[NSThread currentThread]); [operation addExecutionBlock:^{ NSLog( NSBlockOperation2------% ,[NSThread currentThread]); [operation start];
end
打印结果
2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115570] NSBlockOperation1------ NSThread: 0x608000261240 {number 4, name (null)} 2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115571] NSBlockOperation------ NSThread: 0x600000267dc0 {number 3, name (null)} 2017-03-21 11:39:36.710 TestNSOperationQueue[4085:115529] NSBlockOperation2------ NSThread: 0x60800007dc00 {number 1, name main}
注意 只要NSBlockOperation封装的操作数 1,就会异步执行操作
NSOperationQueue的作用 NSOperation可以调用start方法来执行 但默认是同步执行的
如果将NSOperation添加到NSOperationQueue 操作队列 中 系统会自动异步执行NSOperation中的操作
添加操作到NSOperationQueue中 自动执行操作 自动开启线程
NSInvocationOperation * operation1 [[NSInvocationOperation alloc]initWithTarget:self selector: selector(testOperation1) object:nil]; NSInvocationOperation * operation2 [[NSInvocationOperation alloc]initWithTarget:self selector: selector(testOperation2) object:nil]; NSInvocationOperation * operation3 [[NSInvocationOperation alloc]initWithTarget:self selector: selector(testOperation3) object:nil]; //创建NSOperationQueue NSOperationQueue * queue [[NSOperationQueue alloc]init]; //把操作添加到队列中 //第一种方式 [queue addOperation:operation1]; [queue addOperation:operation2]; [queue addOperation:operation3]; //第二种方式 [queue addOperationWithBlock:^{ NSLog( -------testOperationBlock----- }];
[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。
相关文章
- Objective-C 高级编程:iOS与OS X多线程和内存管理
- iOS多线程NSThread的使用
- iOS多线程GCD使用
- 百度面试题——iOS开发
- ios NSURLSession
- 《iOS 6核心开发手册(第4版)》——2.14节构建自定义的锁定控件
- iOS 多线程之dispatch_semaphore(信号量)
- iOS进阶之多线程--NSThread详解
- iOS 多线程:『GCD』详尽总结
- 一篇文章看懂iOS代码块Block
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用
- 关于iOS多线程,你看我就够了
- SwiftUI iOS 多功能TabBar组件支持编程切换 modal隐藏tabview navigationlink隐藏tabview
- iOS流布局UICollectionView使用FlowLayout进行更灵活布局
- js监测设备类型【安卓,ios,苹果微信,电脑pc】
- ios多线程操作(五)—— GCD串行队列与并发队列
- iOS开发-导航栏标题动画
- iOS开发 - 解析tabbar中的凸起按钮
- iOS开发 - 最简洁的用命令行提交代码到Github,加详细说明
- iOS - 仿微信朋友圈视频剪切功能
- 外媒质疑iOS越狱的必要性:已经接近终点了
- iOS多线程