多线程的实现:NSThread

1.子线程的创建:两种方法

第一种: [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];

第二种:NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];

[thread start];

差异:第一种直接方便,第二种在启动前可以对其的stack等参数配置,更具灵活性。

2.在子线程中召唤主线程做事:

[self performSelectorOnMainThread:@selector(函数名) withObject:nil waitUntilDone:YES];

//如果waitUntilDone参数为YES,那么当前线程会被阻拦,直到selector运行完。

3.代码简单示例一:登录

//此为多线程实现登录放UI卡死的代码
//成功进行登录验证后进入到下一ViewController
-(void)presentToNextview{
//到下一界面
} //登录验证
-(void)loginCheck{
//包含POST或GET请求来完成数据的验证,验证成功就跳转到下一界面
if(账号密码匹配){
//召唤主线程跳转到下一界面
[self performSelectorOnMainThread:@selector(presentToNextview) withObject:nil waitUntilDone:YES];
}
} -(void)showindicator{
//显示登录时转圈圈的菊花
} //登录按钮的点击事件
-(IBAction)loginBTN:(id)sender{
//执行的函数有:
[self showindicator]; //开辟新的线程,执行需要联网耗时的函数loginCheck
[NSThread detachNewThreadSelector:@selector(loginCheck) toTarget:self withObject:nil];
}

4.代码示例二:显示一张网上的图片,源代码:

DownloadViewController.h

#import <UIKit/UIKit.h>
#import "FileConnection.h"
@interface DownloadViewController : UIViewController @end

DownloadViewController.m

#import "DownloadViewController.h"
#define kURL @"http://b.hiphotos.baidu.com/image/pic/item/32fa828ba61ea8d37ccb6e0e950a304e241f58ca.jpg"
@interface DownloadViewController () @property UIImageView *imageView; @end UIActivityIndicatorView* activityIndicatorView; @implementation DownloadViewController -(void)downloadImage:(NSString *) url{
NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [[UIImage alloc]initWithData:data];
if(image == nil){
NSLog(@"没有图片");
}else{
NSLog(@"刷新图片");
[ activityIndicatorView stopAnimating ];//停止
//通知主线程做事
[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:NO];
//如果waitUntilDone参数为YES,那么当前线程会被阻拦,直到selector运行完。
}
} -(void)updateUI:(UIImage*) image{
self.imageView.image = image;
//sleep(5);
} -(void)showindicatior{
activityIndicatorView = [ [ UIActivityIndicatorView alloc ] initWithFrame:CGRectMake(,,30.0,30.0)];
activityIndicatorView.activityIndicatorViewStyle= UIActivityIndicatorViewStyleGray;
[self.view addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];//启动 }
- (void)viewDidLoad
{
[super viewDidLoad]; self.imageView=[[UIImageView alloc] initWithFrame:CGRectMake(, , , )]; [self.view addSubview:self.imageView]; //显示菊花
[self showindicatior]; //开辟一个新的线程 2种方法
[NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL];
//NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL];
//[thread start];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

[IOS多线程]的使用:防止进行HTTP数据请求时,UI卡死。的更多相关文章

  1. 当客户端提交更新数据请求时,是先写入edits,然后再写入内存的

    http://blog.sina.com.cn/s/blog_6f83c7470101b7d3.html http://blog.csdn.net/slq1023/article/details/49 ...

  2. iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

    NSURL进行数据请求(POST与GET)   使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以 ...

  3. 当对服务器端返回的极光推送数据请求时,AFN 的 GET 请求失败如何解决

    代码段 控制台   只需在 manager 那里添加一行代码即可 //传入json格式数据,不写则普通post     manager.requestSerializer = [AFJSONReque ...

  4. VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

    正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...

  5. VUE - 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法

     created() {     var that=this     axios.get('http://jsonplaceholder.typicode.com/todos')     .then( ...

  6. bootstrap table分页,重新数据查询时页码为当前页问题

    问题描述: 使用bootstrap table时遇到一个小问题,第一次查询数据未5页,翻页到第5页后,选中条件再次查询数据时,传到后端页码仍旧为5,而此时数据量小于5页,表格显示为未查询到数据. 处理 ...

  7. iOS多线程主题

    下面是:2个并发进程.和2个并发线程的示意图: 下面介绍三种多线程技术(Thread.Cocoa Operation.Grand Central Dispatch): 1.最轻量级Thread(需要自 ...

  8. iOS多线程技术方案

    iOS多线程技术方案 目录 一.多线程简介 1.多线程的由来 2.耗时操作的模拟试验 3.进程和线程 4.多线程的概念及原理 5.多线程的优缺点和一个Tip 6.主线程 7.技术方案 二.Pthrea ...

  9. iOS多线程到底不安全在哪里?

    iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题. 共享状态,多线程共同访问某个对象的property,在iOS编程里是很普遍的使用场景,我们就从P ...

随机推荐

  1. Android 的图片异步请求加三级缓存 ACE

    使用xUtils等框架是很方便,但今天要用代码实现bitmapUtils 的功能,很简单, 1 AsyncTask请求一张图片 ####AsyncTask #####AsyncTask是线程池+han ...

  2. springmvc集成shiro登录失败处理

    一般的登录流程会有:用户名不存在,密码错误,验证码错误等.. 在集成shiro后,应用程序的外部访问权限以及访问控制交给了shiro来管理. shiro提供了两个主要功能:认证(Authenticat ...

  3. struts2 Result Type四个常用转跳类型

    Result的四个常用转跳类型分别为 Dispatcher 用来转向页面,是Struts的默认形式 Redirect   重定向到一个URL Chain  用来处理Action链 RedirectAc ...

  4. Theano3.5-练习之深度卷积网络

    来源:http://deeplearning.net/tutorial/lenet.html#lenet Convolutional Neural Networks (LeNet) note:这部分假 ...

  5. sublime 函数跳转插件 — ctags 安装和使用

    ctags 是 sublime 下一个函数跳转的插件,可以让你方便地从函数调用的位置跳到函数定义的位置.相对于其他插件,ctags 的安装稍微有点复杂,这里记录下备忘. 首先,假设已经安装 Packa ...

  6. php基础入门

    一.序言 由于新公司的需要,我也就从原来的asp专向了,php的学习中.希望通过自己的学习能够尽快的熟悉了解php 二.php独特的语法特色  1.引号问题 在php中单引号和双引号的作用基本相同,但 ...

  7. 日志记录类库log4net的使用总结

    log4net是一个开源的日志记录类库,经过配置后可以自动抓取程序中的错误.异常信息,并写入磁盘,也可以在异常发生时执行其他指定的操作,比如:通知某人右键.写入数据库等.这里写个ASP.NET MVC ...

  8. DatePicker及其监听

    xml文件: <DatePicker android:id="@+id/datep" android:layout_width="wrap_content" ...

  9. linux下PHP7环境搭建

    LAMP环境版本 操作系统:Centos 7 Mysql:5.7.11 Apache:2.4.18 PHP:7.0.4     安装Mysql   下载链接:http://dev.mysql.com/ ...

  10. JVM执行Java程序时内存的划分

    Java虚拟机在执行Java程序过程中会把它所管理的内存区域划分为若干个不同的数据区域. Java虚拟机所管理的内存包括以下几个运行时区域: 1.程序计数器(Program Couter Regist ...