iOS开发多线程篇-NSThread
2023-09-27 14:25:57 时间
上篇我们学习了iOS多线程解决方式中的NSOperation,这篇我主要概况总结iOS多线程中NSThread的解决方式和基本用例
一.iOS多线程对比1. NSThread
[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。
一.iOS多线程对比1. NSThread
每个NSThread对象对应一个线程 真正最原始的线程
自带线程管理的抽象类
Grand Central Dispatch 是Apple开发的一个多核编程的解决方法。
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
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。
相关文章
- iOS多线程GCD使用
- iOS开发基础知识:Core Animation(核心动画)
- 一步一步教你实现iOS音频频谱动画(一)
- iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用
- iOS开发 之 不要告诉我你真的懂isEqual与hash!
- SwiftUI iOS 开源组件之FlashCard旋转卡片slider(教程含源码)
- 解决iOS开发中不合法的网络请求地址
- iOS 实现时间线列表效果
- iOS开发系列--无限循环的图片浏览器
- 开始学习 IOS 开发,学习环境搭建
- iOS - 开发一套代码多个app展示不同图标和名称
- iOS - 开源框架、项目和学习资料汇总(动画篇)