多线程的实现: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. lecture14-RBM的堆叠、修改以及DBN的决策学习和微调

    这是Hinton的第14课,主要介绍了RBM和DBN的东西,这一课的课外读物有三篇论文<Self-taught learning- transfer learning from unlabele ...

  2. SQL Server Management Studio无法记住密码

    用sa账户登录sql server 2008,勾选了“记住密码”,但重新登录时,SQL Server Management Studio无法记住密码.   后来发现,在重新登录时,登录名显示的并非是s ...

  3. Android开发:在EditText中关闭软键盘 转来的

    1.EditText有焦点(focusable为true)阻止输入法弹出 editText=(EditText)findViewById(R.id.txtBody); editText.setOnTo ...

  4. OFFSET IN 使用举例

    本文将结合具体实例阐述OFFSET IN的使用方法.注意:这是我第一次写OFFSET IN约束,本文仅供参考.阅读本文前需要了解时序收敛的基本概念,OFFSET IN和Period的相关知识,可先阅读 ...

  5. WebConfig 详解

    一.Web.Config继承特性 首先我们就来看看配置文件的继承层次.都知道在ASP.NET中有很多的配置文件,如machine.config,web.config,特别是web.config出现在很 ...

  6. .NET基于Redis缓存实现单点登录SSO的解决方案

    一.基本概念 最近公司的多个业务系统要统一整合使用同一个登录,这就是我们耳熟能详的单点登录,现在就NET基于Redis缓存实现单点登录做一个简单的分享. 单点登录(Single Sign On),简称 ...

  7. 2014 todo list

    1. 做好已知的各种项目,争取能成立固定团队2. 横向扩展技术学习,了解各种技术,加强技术素养3. 争取找个妹子4. 加强音律学习5. 继续发展各业余爱好6. 努力摇号 年底看收成.

  8. memcache 安装

    1 下载两个文件 wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz wget http://www.monkey.org/ ...

  9. C语言输入输出整数

    scanf("%llu", &x); printf("%llu\n", x); scanf("%u", &x); print ...

  10. Centos|Rhel搭建vsftp

    vsftp,在ftp传输中相对安全的 01.安装vsftpd yum install -y vsftpd 版本信息: Installed PackagesName        : vsftpdArc ...