zl程序教程

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

当前栏目

iOS开发多线程篇-NSThread

2023-09-27 14:25:57 时间
上篇我们学习了iOS多线程解决方式中的NSOperation,这篇我主要概况总结iOS多线程中NSThread的解决方式和基本用例
一.iOS多线程对比1. NSThread


每个NSThread对象对应一个线程 真正最原始的线程


优点 NSThread轻量级最轻 相对简单


缺点 手动管理所有的线程活动 如生命周期、线程同步、睡眠等


2. NSOperation


自带线程管理的抽象类


优点 自带线程周期管理 操作上可更注重自己逻辑


缺点 面向对象的抽象类 只能实现它或者使用它定义好的两个子类 NSInvocationOperation和NSBlockOperation


3. GCD


Grand Central Dispatch 是Apple开发的一个多核编程的解决方法。


优点 最高效 避开并发陷阱


缺点 基于C实现


4. 选择小结


简单而安全的选择NSOperation实现多线程即可


处理大量并发数据 又追求性能效率的选择GCD


NSThread较麻烦 不建议使用


二. 场景选择图片异步加载 这种常见的场景是最常见也是必不可少的 异步加载图片又分成两种


在UI主线程开启新线程按顺序加载图片 加载完成刷新UI


依然是在主线程开启新线程 顺序不定的加载图片 加载完成后刷新UI


创作工具上的异步 这个跟上边任务调度道理差不多 只是为了丰富描述 有助于“举一反三”效果 如下描述的是APP创作小说


app本地创作10个章节内容完成同步服务器 接着同时发表这10个章节将产生的一系列动作 其中上传内容 获取分配章节Id 如果后台没有做处理最好方式做异步按顺序执行。


app本地创作列表中有3本小说要发表 如果同时发表创作列表中的3本小说 自然考虑并行队列执行发表。


三.使用方法1. 三种实现开启线程方式


动态实例化

NSThread * thread [[NSThread alloc] initWithTarget:self selector: selector(loadImageSource:) object:imgUrl];

 thread.threadPriority //设置线程的优先级 0.0 - 1.0 1.0最高级 

 [thread start];


静态实例化

[NSThread detachNewThreadSelector: selector(loadImageSource:) toTarget:self withObject:imgUrl];


隐式实例化

[self performSelectorOnMainThread: selector(loadImageSource:) withObject:self waitUntilDone:imgUrl];


代码示例

//

// ViewController.m

// TestNSThread

// Created by taobaichi on 2017/3/21.

// Copyright © 2017年 MaChao. All rights reserved.

#import ViewController.h 

#define imgUrl http://avatar.csdn.net/2/C/D/1_totogo2010.jpg 

 interface ViewController ()

 property (nonatomic, strong) UIImageView * imageView;

 implementation ViewController

- (void)viewDidLoad {

 [super viewDidLoad];

 self.imageView [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width /2 - 100, self.view.frame.size.height / 2 - 100, 200, 200)];

 self.imageView.backgroundColor [UIColor purpleColor];

 [self.view addSubview:self.imageView];

 [self dynamicCreateThread];

//动态创建线程

-(void)dynamicCreateThread{

 NSThread * thread [[NSThread alloc] initWithTarget:self selector: selector(loadImageSource:) object:imgUrl];

 thread.threadPriority //设置线程的优先级 0.0 - 1.0 1.0最高级 

 [thread start];

//静态创建线程

-(void)staticCreateThread{

 [NSThread detachNewThreadSelector: selector(loadImageSource:) toTarget:self withObject:imgUrl];

//隐式创建线程

-(void)implicitCreateThread{

 [self performSelectorInBackground: selector(loadImageSource:) withObject:imgUrl];

-(void)loadImageSource:(NSString *)url

 NSData * imgData [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

 UIImage * image [UIImage imageWithData:imgData];

 if (imgData ! nil) {

 //回到主线程刷新UI

 [self performSelectorOnMainThread: selector(refreshImageView:) withObject:image waitUntilDone:YES];

 }else{

 NSLog( there no image data 

-(void)refreshImageView:(UIImage *)image{

 [self.imageView setImage:image];

 end


2. NSThread的拓展认识


获取当前线程

NSThread * current [NSThread currentThread];


获取主线程

NSThread * main [NSThread mainThread];


暂停当前线程

[NSThread sleepForTimeInterval:2.0];


线程之间通信

//在指定线程上执行

 [self performSelector: selector(refreshImageView:) onThread:thread withObject:image waitUntilDone:YES];

 //在主线程执行

 [self performSelectorOnMainThread: selector(refreshImageView:) withObject:image waitUntilDone:YES];

 //在后台执行

 [self performSelectorInBackground: selector(refreshImageView:) withObject:image];

 //在当前 线程上执行

 [self performSelector: selector(refreshImageView:) withObject:image];



[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。