zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

iOS开发多线程篇 - NSOperation(上)

2023-09-27 14:25:57 时间
一. NSOperation简介1. 简单说明


NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程


NSOperation和NSOperationQueue实现多线程的具体步骤


先将需要执行的操作封装到一个NSOperation对象中


然后将NSOperation对象添加到NSOperationQueue中


系统会自动将NSOPerationQueue中的NSOperation取出来


将取出的NSOperation封装的操作放到一条新线程中执行


2.NSoperation的子类


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


使用NSOperation子类的方式有三种


NSInvocationOperation


NSBlockOperation


自定义子类继承NSOperation,实现内部相应的方法


二.具体说明1.NSInvocationOperation子类


创建对象和执行操作

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中 才会以异步执行


2.NSBlockOperation子类


1. 创建对象和添加操作:

NSBlockOperation * operation [NSBlockOperation blockOperationWithBlock:^{

 //......

 operation addExecutionBlock:^{

 //......

 };


2. 代码示例


代码1

//

// 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}


代码2

//

// 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,就会异步执行操作


3.NSOperationQueue


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。